为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

回首忆惘然 2019-05-24 15:13:55
如何在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,获取用户对象,并将它们与注释的结果合并。好像我做错了。
查看完整描述

4 回答

?
手掌心

TA贡献1942条经验 获得超3个赞

我们可以使用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" : [ ]}

我希望这条线可以帮到你。


查看完整回答
反对 回复 2019-05-24
?
千万里不及你

TA贡献1784条经验 获得超9个赞

你必须按照你描述的方式去做。MongoDB是一个非关系型数据库,不支持连接。


查看完整回答
反对 回复 2019-05-24
  • 4 回答
  • 0 关注
  • 1079 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信