Alternatives of Join in MongoDB

╄→гoц情女王★ 提交于 2019-12-23 06:56:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!