0

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: e.g. Player 4 plays how many games with games names.

Parveen Sachdeva
  • 989
  • 2
  • 19
  • 28
  • You cant query two collection at once. First get player number from Players collection than in callback run query to get games for that player. Hope this helps. – Mykola Borysyuk Aug 08 '16 at 08:14
  • @MykolaBorysyuk - Please see $lookup - this feature in the aggregate pipeline is capable of doing outer joins - https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/ – Clement Amarnath Aug 08 '16 at 08:38
  • Possible duplicate of [MongoDB query multiple collections at once](https://stackoverflow.com/questions/6502541/mongodb-query-multiple-collections-at-once) – Guilherme Ferreira Sep 23 '19 at 18:24

1 Answers1

3

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

Clement Amarnath
  • 5,301
  • 1
  • 21
  • 34