How do I perform the SQL Join equivalent in MongoDB? -


how perform sql join equivalent in mongodb?

for example have 2 collections (users , comments) , want pull comments pid=444 along user info each.

comments   { uid:12345, pid:444, comment="blah" }   { uid:12345, pid:888, comment="asdf" }   { uid:99999, pid:444, comment="qwer" }  users   { uid:12345, name:"john" }   { uid:99999, name:"mia"  } 

is there way pull comments field (eg. ...find({pid:444}) ) , user information associated each comment in 1 go?

at moment, first getting comments match criteria, figuring out uid's in result set, getting user objects, , merging them comment's results. seems doing wrong.

as of mongo 3.2 answers question no longer correct. new $lookup operator added aggregation pipeline identical left outer join:

https://docs.mongodb.org/master/reference/operator/aggregation/lookup/#pipe._s_lookup

from docs:

{    $lookup:      {        from: <collection join>,        localfield: <field input documents>,        foreignfield: <field documents of "from" collection>,        as: <output array field>      } } 

of course mongo not relational database, , devs being careful recommend specific use cases $lookup, @ least of 3.2 doing join possible mongodb.


Comments