简体   繁体   中英

MongoDB - Aggregating multiple collections into a single collection under different keys

I have a MongoDB database with about 100 collections. The collections are very similar to each in structure, but the data is different enough that I need to keep the entries in the collections separate. So I am trying to figure out how I can aggregate all the individual collections into a single collection under different keys .

For example, my database currently has the collections:

collection_set_A
collection_set_B
collection_set_C
collection_set_D
...

and I would like to have a single collection that looks like this:

collection_set
  |
  +-collection_set_A
  +-collection_set_B
  +-collection_set_C
  +-collection_set_D
  +-...

So that collection_set_A can now be accessed as collection_set['collection_set_A'] .

Is this possible? I have seen a lot of references to map/reduce , but those seem to be more for joining data, and not full collections like this. I'm basically wanting to move data, not join it.

Does anyone know if this is possible?

This is not possible, but a work-around would be to use the copyto method db.collection.copyTo() to copy the contents of one collection to another collection

You can find more information here http://docs.mongodb.org/manual/reference/method/db.collection.copyTo/#db.collection.copyTo

A somewhat similar question is answered here Can a MongoDB collection have inside it another collection?

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