问题
I am new in mongoDB. I am facing Problem with mongoDB. That is "How do i give relationship between two collections in mongoDB?"
I read manual of mongoDB that mention mongoDB is not a RDBMS and mongoDB is not support Any kind of JOINS. But i want to know Alternatives of JOINS in mongoDB.
回答1:
From MongoDb v3.2 , new $lookup operator was introduced to perform joins in MongoDb . This works as sql left join and its documentation can be found at
https://docs.mongodb.com/master/reference/operator/aggregation/lookup/#pipe._S_lookup
回答2:
You can get data from two or more collection use aggregate method suing $lookup. db.test.aggregate([{$lookup:{from:"test1",localField:"user_id",foreignField:"emp_id",as:"data"}}]).pretty(); Here test is 1st collection, test1 is 2nd collection and user_id and emp_id are join key from respective collections. And we will get result data in data key as the result.
来源:https://stackoverflow.com/questions/32817900/alternatives-of-join-in-mongodb