亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在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 關注
  • 1193 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號