Reputation: 82
I am using node js and mongo db and I want to pass a variable to collections
var id = "someid";
db.collection(id).insert("some json data");
If I did like this it is giving me an error as collection name must be a string.
Upvotes: 5
Views: 3243
Reputation: 1525
You can create variable using var for collection name
var colName = "mytest"
and then execute all the operations on collections as below:
db[colName].find()
db[colName].rename("newName")
etc. This will help you keep your collection name dynamic and can even update it keeping your commands same.
Hope this helps!
Upvotes: 9