2
votes

I have a mongo db collection named like "name.types". When I am creating model for the collection in loopback, I cannot enter the model name with the "." as it says special characters not allowed. So I created the model as "name_types". Now how can I connect this model to the collection "name.types"? Any help would be appreciated. Thanks!

2

2 Answers

2
votes

You can set collection name in model.json file :

//model.json

...
"options": {
    "validateUpsert": true,    
    "mongodb": {
      "collection": "name_types"
    }
  },
....
0
votes

You can define a different collection name for your existing model by passing an option in model definition, something like this

    Post = db.define('Post', {
      title: { type: String },
      content: { type: String }
    }, 
    {
      mongodb: {
        collection: 'PostCollection', // Custom the collection name
       }
   });

You can do it from the model.json file or from a boot script. Good Luck.. :)