簡體   English   中英

如何使用節點js從mongodb的3個集合中獲取數據?

[英]how to get data from 3 collections in mongodb using node js?

我在mongodb中有以下收藏

db.orders.find()

{ "_id" : ObjectId("5cc69ad493297eade15bacb7"), "item" : "card", "qty" : 15, "user_mob" : 8220097 }
{ "_id" : ObjectId("5cc69adf93297eade15bacb8"), "item" : "card1", "qty" : 11, "user_mob" : 8220097 }

db.items.find()

{ "_id" : ObjectId("5cc69ba493297eade15bacbc"), "item" : "card1", "color" : "blue", "weight" : "50g" }
{ "_id" : ObjectId("5cc69bb793297eade15bacbd"), "item" : "card", "color" : "yellow", "weight" : "60g" }
{ "_id" : ObjectId("5cc69bd193297eade15bacbe"), "item" : "ball", "color" : "multi color", "weight" : "140g" }
{ "_id" : ObjectId("5cc69be793297eade15bacbf"), "item" : "footer", "color" : "Black", "weight" : "340g" }
{ "_id" : ObjectId("5cc69c0c93297eade15bacc0"), "item" : "caps", "color" : "multi color", "weight" : "250g" }

db.users_nippon.find()

{ "_id" : ObjectId("5cadf1d9b0a2d18bdc6de90a"), "name" : "hariharan", "mobile_no" : "8220097", "role_id" : "2", "language_code" : "ml", "encrypted_password" : "password_Passw0rd", "is_salesman" : "true", "is_mobile" : "true" }

我在節點js中編寫代碼以從上述所有表中獲取數據。 代碼是...

db.collection('users_nippon').aggregate([
            {
                $lookup: {
                   from: "orders",
                   localField: "mobile_no",
                   foreignField: "user_mob",
                   as: "orders_data"
                }
            },
            {
                $unwind: "$orders_data"
            },
            {
                $lookup: {
                    from: "items",
                    localField: "item",
                    foreignField: "item",
                    as: "items_data"
                }
            },
            {
                $unwind: "$items_data"
            }
        ]).toArray(function(err, list) {
            if (err) throw err;
            console.log(JSON.stringify(list));
            res.send(JSON.stringify(list));
            });

出了點問題。 它返回空( [] )。

因此,請幫助我解決此問題。

下面的代碼是我想要的輸出的示例

[
{
    "_id": "5cc67e439c26e35a70fddfd5",
    "name": "hariharan",
    "mobile_no": "8220097",
    "role_id": "2",
    "language_code": "ml",
    "encrypted_password": "password_Passw0rd",
    "is_salesman": "true",
    "is_mobile": "true",
    "orders_data": {
        "_id": "5cc6ebe9f4c1d9080b93b013",
        "item": "card",
        "qty": 15,
        "user_mob": "8220097",
        "items_data": {
                "_id": "5cc69bb793297eade15bacbd",
                "item": "card",
                "color": "yellow",
                "weight": "60g"
          }
    }

}]

您的查詢有2個問題:

  1. 字段user_mob (訂單模式)是一個Numbermobile_no (user_nippon模式)是一個String因此無法進行查找。 您可能要對這些字段使用相同的類型。

  2. 第二次lookup不正確。 第一次lookupunwind將在orders_data元素內返回您的item元素,因此此lookuplocalField應該更改為: orders_data.item

因此,在將user_mobmobile_no都更改為具有匹配類型之后,您的查詢將如下所示:

db.collection('users_nippon').aggregate([
            {
                $lookup: {
                   from: "orders",
                   localField: "mobile_no",
                   foreignField: "user_mob",
                   as: "orders_data"
                }
            },
            {
                $unwind: "$orders_data"
            },
            {
                $lookup: {
                    from: "items",
                    localField: "orders_data.item",
                    foreignField: "item",
                    as: "items_data"
                }
            },
            {
                $unwind: "$items_data"
            }
        ]).toArray(function(err, list) {
            if (err) throw err;
            console.log(JSON.stringify(list));
            res.send(JSON.stringify(list));
            });

您必須將users_nippon集合中mobile_no數據類型從string更改為NumberLong否則$lookup將不起作用。 更正它之后,現在您可以使用下面的聚合查詢以所需的相同格式獲得所需的結果。

db.collection('users_nippon').aggregate([

        {
            $lookup: {
              from: "orders",
              localField: "mobile_no",
              foreignField: "user_mob",
              as: "orders_data"
            }
        },
        {
            $unwind: "$orders_data"
        },
        {
            $lookup: {
                from: "items",
                localField: "orders_data.item",
                foreignField: "item",
                as: "orders_data.items_data"
            }
        },
        {
            $unwind: "$orders_data.items_data"
        }
    ]).toArray(function(err, list) {
        if (err) throw err;
        console.log(JSON.stringify(list));
        res.send(JSON.stringify(list));
        });

這是經過測試的有效解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM