gpt4 book ai didi

node.js - 如何使用 Node js从mongodb中的3个集合中获取数据?

转载 作者:可可西里 更新时间:2023-11-01 09:32:47 25 4
gpt4 key购买 nike

I have the below collections in 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"
}
}

}]

最佳答案

您的查询中有 2 个问题:

  1. 字段user_mob(订单模式)是一个数字,而mobile_no(user_nippon 模式)是一个字符串 因此无法进行查找。您可能希望对这些字段使用相同的类型。

  2. 第二个查找不正确。第一个 lookupunwind 返回 orders_data 元素内的 item 元素,所以 localFieldlookup 应更改为: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));
});

关于node.js - 如何使用 Node js从mongodb中的3个集合中获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55902543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com