简体   繁体   中英

MongoDB Query from Two collections

I have two collections Players and Games: i. Players contains player id from 1-15. ii. Games contains games name: Hockey Cricket Badminton

with player id from 1-11 from "Players" collection.

Query for fetching all the games played by a specific player: eg Player 4 plays how many games with games names.

This can be done using $lookup - which can perform outer join on two collections, but you need mongodb version of 3.2 and also the two collections in your scenario Players and Games should be in same database.

References

https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/

https://www.mongodb.com/blog/post/joins-and-other-aggregation-enhancements-coming-in-mongodb-3-2-part-2-of-3-worked-examples

http://www.codeproject.com/Articles/1077839/Working-with-MongoDBs-lookup-Aggregator

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