简体   繁体   English

导入多个集合(nodejs,mongodb)

[英]Import multiple collections (nodejs, mongodb)

I'm new with node.js and mongodb. 我是node.js和mongodb的新手。 In my database, I have several collections (one for users, one for 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. 在我的server.js文件中,我希望能够在每个集合中编写。 Here is the code I use, but I'm not able to access all my collections... Have you an ideas to make that possible? 这是我使用的代码,但我无法访问我的所有馆藏......你有想法使这成为可能吗?

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

Thank you 谢谢

You don't have to know all of your collections in advance to use mongojs , you can access a collection dynamically using db.collection('name_of_collection') and use it just like an existing collection. 您不必事先知道所有集合以使用mongojs ,您可以使用db.collection('name_of_collection')动态访问集合,并像使用现有集合一样使用它。 This call will also cache it so that next time, you can say db.name_of_collection . 此调用也将缓存它,以便下次可以说db.name_of_collection

There's a bunch of examples at their git hub page: https://github.com/gett/mongojs 他们的git hub页面上有很多例子: https//github.com/gett/mongojs

Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM