0

I am working with SailsJs+MongoDB API. I have to create New colletion in mongoDB .Name of colletion will be in request Parameter.

example:

Suppose I want to create 'Users' collection in 'mongoDbDatabase' database by following request.

 {
    "collectionName" : "Users",
    "dbName"    :"mongoDbDatabase"
    }

Now is there any way to create dynamic collection in mongoDB using req.param('collectionName) variable ?

Devang Mistry
  • 595
  • 1
  • 10
  • 22

1 Answers1

1

To use all the tools that Sails provides, you have to add code (before you start your app) for each Model / Collection you are planning to interact with. As a result, creating a collection dynamically will mean you can't take advantage of the whole data - framework sails provides.

Are you sure you need a dynamic collection? Why not a defined collection differentiated by attributes?

If you really need to do this from Sails, it looks like you can get access to the underlying raw mongo database:

var db = AnyModel.getDatastore().manager; // the database will be as defined in config/models.js or config/connections.js

var collectionName = 'Widgets';
db.createCollection(collectionName);

// note, even if this works, something like 'Widgets.find' will not.
arbuthnott
  • 3,819
  • 2
  • 8
  • 21
  • Thanks for your answer , and you are right . That I will not able to use queries of collection provided by sails for dynamic created tables. as per your suggestions , I'll try to make changes in DB Design and will try to work out with attribute differentiation. Thanks – Devang Mistry Mar 29 '18 at 12:43