简体   繁体   中英

How to create multiple collections in mongodb?

I currently have a users collection. I want to create a logs collection and join them together. Right now I can create the users collection through.createCollection. But is it possible to create multiple collections in one go? Or do I have to do it through two separate.createCollection operations?

You can make a list of collection names and then loop through it and call db.createCollection operations on mongo shell easily:

eg:

> var collectionList = ["A", "B"];
> collectionList.forEach(function(collectionName) {db.createCollection(collectionName)})

After completion of the above command, just check the list of all collections:

> show collections
A
B

For details about the create collection visit the official documentation createCollection

And for reference to other collections, please check the documentation of DBRef

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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