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 ?

1 Answer 1

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.
Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.