如何在MongoDB中执行SQL Join等效项?

如何在MongoDB中执行SQL Join等效项?

如何在MongoDB中执行SQL Join等效项?

例如,假设你有两个集合(用户和评论),我想用pid = 444以及每个集合的用户信息来提取所有评论。

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"  }

有没有办法用一个字段拉出所有评论(例如......查找({pid:444}))以及与每个评论相关的用户信息?

目前,我首先得到符合我标准的注释,然后找出该结果集中的所有uid,获取用户对象,并将它们与注释的结果合并。好像我做错了。


回首忆惘然
浏览 1087回答 4
4回答

手掌心

我们可以使用mongodb客户端控制台将只有一行的简单函数合并/加入一个集合中的所有数据,现在我们可以执行所需的查询。下面是一个完整的例子.-作者:db.authors.insert([     {         _id: 'a1',         name: { first: 'orlando', last: 'becerra' },         age: 27     },     {         _id: 'a2',         name: { first: 'mayra', last: 'sanchez' },         age: 21     }]);.-分类:db.categories.insert([     {         _id: 'c1',         name: 'sci-fi'     },     {         _id: 'c2',         name: 'romance'     }]);.-书籍db.books.insert([     {         _id: 'b1',         name: 'Groovy Book',         category: 'c1',         authors: ['a1']     },     {         _id: 'b2',         name: 'Java Book',         category: 'c2',         authors: ['a1','a2']     },]);.-图书借阅db.lendings.insert([     {         _id: 'l1',         book: 'b1',         date: new Date('01/01/11'),         lendingBy: 'jose'     },     {         _id: 'l2',         book: 'b1',         date: new Date('02/02/12'),         lendingBy: 'maria'     }]);。- 魔法:db.books.find().forEach(     function (newBook) {         newBook.category = db.categories.findOne( { "_id": newBook.category } );         newBook.lendings = db.lendings.find( { "book": newBook._id  } ).toArray();         newBook.authors = db.authors.find( { "_id": { $in: newBook.authors }  } ).toArray();         db.booksReloaded.insert(newBook);     });.-获取新的收集数据:db.booksReloaded.find().pretty().-回应:){     "_id" : "b1",     "name" : "Groovy Book",     "category" : {         "_id" : "c1",         "name" : "sci-fi"     },     "authors" : [         {             "_id" : "a1",             "name" : {                 "first" : "orlando",                 "last" : "becerra"             },             "age" : 27         }     ],     "lendings" : [         {             "_id" : "l1",             "book" : "b1",             "date" : ISODate("2011-01-01T00:00:00Z"),             "lendingBy" : "jose"         },         {             "_id" : "l2",             "book" : "b1",             "date" : ISODate("2012-02-02T00:00:00Z"),             "lendingBy" : "maria"         }     ]}{     "_id" : "b2",     "name" : "Java Book",     "category" : {         "_id" : "c2",         "name" : "romance"     },     "authors" : [         {             "_id" : "a1",             "name" : {                 "first" : "orlando",                 "last" : "becerra"             },             "age" : 27         },         {             "_id" : "a2",             "name" : {                 "first" : "mayra",                 "last" : "sanchez"             },             "age" : 21         }     ],     "lendings" : [ ]}我希望这条线可以帮到你。

千万里不及你

你必须按照你描述的方式去做。MongoDB是一个非关系型数据库,不支持连接。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

MongoDB