我有2个收藏夹。一个是资源,另一个是物品
资源:
{
"_id" : "fGR4ECcw5p2HFgxd3",
"ownerId" : "J8MpsWChPQdET6jwQ",
"createdAt" : ISODate("2018-07-04T07:25:28.418Z"),
"inventory" : [
{
"_id" : ObjectId("5b537ef5895adfeae037e0a4"),
"quantity" : 10
},
{
"_id" : ObjectId("5b537d9c895adfeae037dbd2"),
"quantity" : 5
}
]
}
Items Collection中有2个文档:
{
"_id" : ObjectId("5b537d9c895adfeae037dbd2"),
"name" : "Item1",
"useable" : true,
"img" : ""
}
{
"_id" : ObjectId("5b537ef5895adfeae037e0a4"),
"name" : "Item2",
"useable" : false,
"img" : ""
}
如您所见,库存数组中有项目ID。
我想做的是如何编写正确的查询以获取包含每个项目信息的库存数组(来自项目集合)
示例:
"inventory" : [
{
"_id" : ObjectId("5b537ef5895adfeae037e0a4"),
"name":"Item2",
"useable":false,
"img":"",
"quantity" : 10
},
{
"_id" : ObjectId("5b537d9c895adfeae037dbd2"),
"name":"Item1",
"useable":true,
"img":"",
"quantity" : 5
}
]
我正在使用流星黑客聚合软件包: 安东尼·温兹莱特(Antonio Winzlets)尝试 这是服务器端方法:
'getInventory':function(userid){
return Resources.aggregate([
{ "$match": { "ownerId": userid } },
{ "$unwind": "$inventory" },
{ "$lookup": {
"from": Items,
"let": { "inventoryId": "$inventory._id", "quantity": "$quantity" },
"pipeline": [
{ "$match": { "$expr": { "$eq": [ "$_id", "$$inventoryId" ] } } },
{ "$addFields": { "quantity": "$$quantity" } }
],
"as": "inventory"
}},
{ "$unwind": "$inventory" },
{ "$group": {
"_id": "$_id",
"ownerId": { "$first": "$ownerId" },
"createdAt": { "$first": "$createdAt" },
"inventory": { "$push": "$inventory" }
}}
])
}
与Items.collecion.name我不确定 与Items.name相同的错误与只是Items(调用堆栈大小超出) 这是我得到的错误:
W20180721-23:18:22.253(3)? (STDERR) (node:10800) UnhandledPromiseRejectionWarning: RangeError: Maximum call stack size exceeded
W20180721-23:18:22.254(3)? (STDERR) at Array.some (<anonymous>)
W20180721-23:18:22.254(3)? (STDERR) at Object.matchObject (packages/ejson/ejson.js:180:31)
W20180721-23:18:22.254(3)? (STDERR) at toJSONValueHelper (packages/ejson/ejson.js:239:19)
W20180721-23:18:22.254(3)? (STDERR) at Object.EJSON.toJSONValue.item [as toJSONValue] (packages/ejson/ejson.js:292:19)
W20180721-23:18:22.255(3)? (STDERR) at Object.keys.forEach.key (packages/ejson/ejson.js:188:29)
W20180721-23:18:22.255(3)? (STDERR) at Array.forEach (<anonymous>)
W20180721-23:18:22.255(3)? (STDERR) at Object.toJSONValue (packages/ejson/ejson.js:187:24)
W20180721-23:18:22.256(3)? (STDERR) at toJSONValueHelper (packages/ejson/ejson.js:240:24)
W20180721-23:18:22.256(3)? (STDERR) at Object.EJSON.toJSONValue.item [as toJSONValue] (packages/ejson/ejson.js:292:19)
W20180721-23:18:22.257(3)? (STDERR) at Object.keys.forEach.key (packages/ejson/ejson.js:188:29)
W20180721-23:18:22.257(3)? (STDERR) at Array.forEach (<anonymous>)
W20180721-23:18:22.258(3)? (STDERR) at Object.toJSONValue (packages/ejson/ejson.js:187:24)
W20180721-23:18:22.258(3)? (STDERR) at toJSONValueHelper (packages/ejson/ejson.js:240:24)
W20180721-23:18:22.259(3)? (STDERR) at Object.EJSON.toJSONValue.item [as toJSONValue] (packages/ejson/ejson.js:292:19)
W20180721-23:18:22.259(3)? (STDERR) at Object.keys.forEach.key (packages/ejson/ejson.js:188:29)
W20180721-23:18:22.259(3)? (STDERR) at Array.forEach (<anonymous>)
W20180721-23:18:22.260(3)? (STDERR) (node:10800) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
W20180721-23:18:22.261(3)? (STDERR) (node:10800) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
答案 0 :(得分:1)
您可以尝试以下$lookup
聚合
db.resources.aggregate([
{ "$match": { "ownerId": Meteor.userId() } },
{ "$unwind": "$inventory" },
{ "$lookup": {
"from": Items.collection.name,
"let": { "inventoryId": "$inventory._id", "quantity": "$quantity" },
"pipeline": [
{ "$match": { "$expr": { "$eq": [ "$_id", "$$inventoryId" ] } } },
{ "$addFields": { "quantity": "$$quantity" } }
],
"as": "inventory"
}},
{ "$unwind": "$inventory" },
{ "$group": {
"_id": "$_id",
"ownerId": { "$first": "$ownerId" },
"createdAt": { "$first": "$createdAt" },
"inventory": { "$push": "$inventory" }
}}
])