Import multiple collections (nodejs, mongodb)

I am new to node.js and mongodb. In my database, I have several collections (one for users, one for an article, and in the future one more ...).

In my server.js file, I would like to be able to write in each of these collections. Here is the code I use, but I cannot access all my collections. Do you have any ideas to make this possible?

var databaseUrl = "mydb"; // "username: password@example.com /mydb" var collections = ["users", "article", "reports", "archery"] var db = require("mongojs").connect(databaseUrl, collections); 

thanks

+4
source share
1 answer

You do not need to know all your collections in advance to use mongojs , you can dynamically access the collection using db.collection('name_of_collection') and use it just like an existing collection. This call will also cache it so that next time you can say db.name_of_collection .

There are tons of examples on the page.

hub: https://github.com/gett/mongojs

Good luck.

+3
source

All Articles