MongoDB在带有附加字段的对象数组上聚合$ lookup [英] MongoDB Aggregate $lookup on array of object with additional field
问题描述
我有两个不同的集合(以下示例)方法& 项.到目前为止,我正在对$lookup
使用3.6>之前的3.6原始聚合查询:
{
$lookup: {
from: "items",
localField: "reagents._id",
foreignField: "_id",
as: "reagent_items"
}
}
问题是,如果我使用它,则会在原始集合的$lookup
阶段错过quantity
字段(来自嵌入的methods.reagents
).现在,我在lookup
之后返回quantity
,但是正如我所听到的,Mongo从3.6开始为lookup
查询引入了一种新语法,所以问题是:
它能否解决我的问题,但收到以下结果:
{
"_id": 1,
"name": "Test",
"reagent_items": [ // <= the exact schema of what I need after lookup
{
"_id": 1,
"name": "ItemOne",
"other": "field",
"quantity": 2 //quantity field from original {array of objects} after lookup
},
{
"_id": 2,
"name": "ItemTwo",
"other": "field",
"quantity": 4 //quantity field from original {array of objects} after lookup
}
],
"reagents": [ //original reagents field here just for example, we could remove it
{
"_id": 1,
"quantity": 2
},
{
"_id": 2,
"quantity": 4
}
]
}
方法
{
"_id": 1,
"name": "Test",
"reagents": [
{
_id: 1,
quantity: 2
},
{
_id: 2,
quantity: 4
}
]
}
项目
{
"_id": 1,
"name": "ItemOne",
"other": "field"
},
{
"_id": 2,
"name": "ItemTwo",
"other": "field"
}
使用 $ arrayElemAt 为每个 I have two different collections (example below) methods & items. As for now, I'm using pre 3.6 vanilla aggregation query for The problem is that if I am using it, I miss Can it solve my problem for receiving the following results:
methods items
Use $map along with $arrayElemAt to find corresponding 这篇关于MongoDB在带有附加字段的对象数组上聚合$ lookup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!reagent_items
查找相应的reagent
并应用$lookup
: {
$lookup: {
from: "items",
localField: "reagents._id",
foreignField: "_id",
as: "reagent_items"
}
}
quantity
field (from methods.reagents
embedded) during $lookup
stage from original collection. For now, I return quantity
right after lookup
but as I heard, Mongo introduced from 3.6 a new syntax for lookup
queries, so the question is:
{
"_id": 1,
"name": "Test",
"reagent_items": [ // <= the exact schema of what I need after lookup
{
"_id": 1,
"name": "ItemOne",
"other": "field",
"quantity": 2 //quantity field from original {array of objects} after lookup
},
{
"_id": 2,
"name": "ItemTwo",
"other": "field",
"quantity": 4 //quantity field from original {array of objects} after lookup
}
],
"reagents": [ //original reagents field here just for example, we could remove it
{
"_id": 1,
"quantity": 2
},
{
"_id": 2,
"quantity": 4
}
]
}
{
"_id": 1,
"name": "Test",
"reagents": [
{
_id: 1,
quantity: 2
},
{
_id: 2,
quantity: 4
}
]
}
{
"_id": 1,
"name": "ItemOne",
"other": "field"
},
{
"_id": 2,
"name": "ItemTwo",
"other": "field"
}
reagent
for each reagent_items
and the apply $mergeObjects to get one object:db.methods.aggregate([
{
$lookup: {
from: "items",
localField: "reagents._id",
foreignField: "_id",
as: "reagent_items"
}
},
{
$project: {
_id:1,
name: 1,
reagents: 1,
reagent_items: {
$map: {
input: "$reagent_items",
as: "ri",
in: {
$mergeObjects: [
"$$ri",
{
$arrayElemAt: [ { $filter: { input: "$reagents", cond: { $eq: [ "$$this._id", "$$ri._id" ] } } }, 0 ]
}
]
}
}
}
}
}
])