简体   繁体   中英

union two collections in mongodb with one query

I have one collection named USER

{"_id" => "id1", ...}

and another collection named CONTACT

{"_id" => "id2", ...}

now i have an array

[id1, id2]

Can I get two data with one query?

You can use the Aggregation Framework $setUnion operator for that.

db.collection.aggregate(
   [
     { $project: { id1:1, id2: 1, allValues: { $setUnion: [ "$id1", "$id2" ] } } }
   ]
)

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