【发布时间】:2021-04-19 05:12:33
【问题描述】:
我对 MongoDB 查询和练习 mongoDb 查询真的很陌生。我正在使用这个官方mongoDb-package。我正在关注这个doc。我有三个数据集合。一个是包包,一个是卖家,最后一个是布料。我们的数据架构是当卖家发送带有他/她信息的布袋时。我们创建了这样的卖家集合:
{ "sellerId": 1234,
"firstName": "John",
"lastName": "doe",
"fullName": "John Doe",
"email": "john@yahoo.com",
"bagId": 2224
}
这是包集合
{
"sellerId": 1234
"bagId": 2224,
"source" : "fedex"
}
从袋子里挑选出打算出售的布料后,我们创建布料系列。
[
{
"bagId": 2224,
"brandName": "Denim",
"size": "32",
"clothId": 1244,
"color": "green",
"price": 20
},
{
"bagId": 2224,
"brandName": "Zara",
"size": "31",
"clothId": 1243,
"color": "red",
"price": 90
}
]
当布料从 Shopify 售出时。我们得到 SKU-ID 数组,这是我们的布料集合clothId。
我的目标是当布料售出时,我们将clothId(SKU-ID FROM SHOPIFY) 匹配到find bagId,从那个bagId我们会得到卖家信息。
我的预期结果是
{
"firstName": "John",
"lastName": "doe",
"fullName": "John Doe",
"email": "john@yahoo.com",
"bagId": 2224,
"clothId": 1244 // sold cloth id which I got from Shopify
"sellerId": 1234
}
我成功匹配了售出的布料 id 和 shopify (SKU-ID) 并获取了包包信息,但我无法弄清楚如何从 bagId 获取卖家信息
这是我获取已售布料信息和包包详细信息的代码,但它没有给我卖家信息,只是得到了空数组
const sllerInfo = await client
.db()
.collection('clothes')
.aggregate(
[
{ $match: { clothId: { '$in': convertInto } } }, // convertInto is arrays of sold clothId which I got from Shopify
{
$lookup:
{
from: "bags",
localField: "bagId",
foreignField: "bagId",
as: "bags"
}
},
{
$lookup:
{
from: "sellers",
localField: "sellerId",
foreignField: "sellerId",
as: "sellers"
},
},
{
"$project": {
"bagId": 1.0,
"bags.source": 1.0,
"sellers.firstName": 1.0, // dont get anything
"sellers.lastName": 1.0, // dont get anything
"brand": 1.0
}
},
]
).toArray()
【问题讨论】:
标签: mongodb mongodb-query aggregation-framework