在MongoDB中加入的替代方法 [英] Alternatives of Join in MongoDB
问题描述
我是mongoDB的新手. 我正面临mongoDB的问题.那就是"如何在mongoDB中确定两个集合之间的关系?"
I am new in mongoDB. I am facing Problem with mongoDB. That is "How do i give relationship between two collections in mongoDB?"
我阅读了mongoDB的手册,其中提到mongoDB不是RDBMS,并且mongoDB不支持任何一种JOINS.但是我想知道mongoDB中的JOINS的替代方案.
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.
推荐答案
您可以使用汇总方法使用$ lookup从两个或多个集合中获取数据. db.test.aggregate([[$$ lookup:{from:"test1",localField:"user_id",foreignField:"emp_id",as:"data"}}]]).pretty(); 这里的test是第一个集合,test1是第二个集合,user_id和emp_id是来自各个集合的联接关键字.然后我们将结果数据作为数据键.
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.
这篇关于在MongoDB中加入的替代方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!