【问题标题】:how to get data from 3 collections in mongodb using node js?如何使用节点 js 从 mongodb 中的 3 个集合中获取数据?
【发布时间】:2019-09-18 00:42:57
【问题描述】:

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

我在 node 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"
          }
    }

}]

【问题讨论】:

  • 你在这个console.log(JSON.stringify(list));上得到空值了吗?
  • 没有。我得到空数组[]
  • 从上述代码中删除JSON.Stringify和两个$unwind阶段并检查响应。
  • 它返回一些数据。但是,返回数据的结构不是格式。 [ {“_id”:“5cadf1d9b0a2d18bdc6de90a”,“名称”:“hariharan”,“mobile_no”:“8220097”,“role_id”:“2”,“language_code”:“ml”,“encrypted_pa​​ssword”:“password_Passw0rd”, “is_salesman”:“true”,“is_mobile”:“true”,“orders_data”:[],“items_data”:[] } }
  • 你的收藏链接了吗,你能给我们看看你的 users_nippon 模型吗

标签: node.js mongodb mongodb-query


【解决方案1】:

您必须将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));
        });

这是经过测试且有效的解决方案。

【讨论】:

  • 现在它正在工作,兄弟。感谢您的回答。你救我。非常感谢。
【解决方案2】:

您的查询中有 2 个问题:

  1. 字段user_mob(订单模式)是Number,而mobile_no(用户日本模式)是String,因此无法进行查找。您可能希望对这些字段使用相同的类型。

  2. 第二个lookup 不正确。第一个lookupunwindorders_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));
            });

【讨论】:

  • 你的代码很有用..它给了我一些输出。但我需要不同 Json 格式的输出。请检查输出格式的问题。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-03
  • 2020-07-21
  • 2014-07-16
  • 1970-01-01
  • 2019-08-02
  • 2018-08-16
  • 2015-10-07
相关资源
最近更新 更多