Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

variable collection name in mongodb shell using javascript

Using the mongo shell which is javascript:

db.collection.insert() 

Can I allow the collections name to be dynamic so as to work with several collections?

like image 622
prashantas Avatar asked Nov 29 '22 00:11

prashantas


1 Answers

From the mongo shell:

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!

like image 84
Satish Avatar answered Dec 06 '22 11:12

Satish