我有一段代码将集合A(样本)加入集合B(定位器)。我尝试过$unwind
,$group
和$push
语法唯一的问题是我无法返回字段locator
和record
。
data = db.sample.aggregate([
{'$lookup': {
'from': 'locators',
'localField': "locator",
'foreignField': "_id",
'as': "metalocator"}}])
print(list(data))
返回
[
{
'_id': '599A65E1A80541BA',
'locator': 'ABC',
'record': 'Nicaragua',
'metalocator': [{'_id': 'ABC', 'group': 'Location', 'section': 'Geo', 'cabinet': 'Country', 'record': 'string', 'status': 'integer'}]
},
{
'_id': '428E970995AE8C76',
'locator': 'CDE',
'record': 'Nigeria',
'metalocator': [{'_id': 'CDE', 'group': 'Location', 'section': 'Geo', 'cabinet': 'Country', 'record': 'string', 'status': 'integer'}]
}
]
data = db.sample.aggregate([
{"$lookup": {"from": "locators",
"localField": "locator",
"foreignField": "_id",
"as": "metalocator"}},
{"$unwind": '$metalocator'},
{"$group": {"_id": "$_id",
"metalocator": {"$push": {
"section": "$metalocator.section",
"cabinet": "$metalocator.cabinet"}}}}
])
print(list(data))
返回:
[
{
'_id': '1835853D2982AAEF',
'metalocator': [{'section': 'Geo', 'cabinet': 'Country'}]
},
{
'_id': '428E970995AE8C76',
'metalocator': [{'section': 'Geo', 'cabinet': 'Country'}]
}
]
[
{
'_id': '1835853D2982AAEF',
'locator': 'ABC',
'record': 'Nicaragua',
'metalocator': [{'section': 'Geo', 'cabinet': 'Country'}]
},
{
'_id': '428E970995AE8C76',
'locator': 'CDE',
'record': 'Nigeria',
'metalocator': [{'section': 'Geo', 'cabinet': 'Country'}]
}
]
答案 0 :(得分:3)
您想要$map
:
db.sample.aggregate([
{'$lookup': {
'from': 'locators',
'localField': "locator",
'foreignField': "_id",
'as': "metalocator"
}},
{ '$addFields': {
'metalocator': {
'$map': {
'input': '$metalocator',
'in': {
'section': '$$this.section',
'cabinet': '$$this.cabinet'
}
}
}
}}
])
这就是你用来重新映射"数组的内容,这正是你所要求的。它的用法与python中许多同名的运算符以及许多其他语言大致相同。
如果你有MongoDB 3.6,你可以选择使用不同的$lookup
语法,你可以选择"选择"哪些字段从那里返回:
db.sample.aggregate([
{'$lookup': {
'from': 'locators',
'let': { 'locator': '$locator' },
'pipeline': [
{ '$match': {
'$expr': { '$eq': [ '$_id', '$$locator' ] }
}},
{ '$project': { 'section': 1, 'cabinet': 1, '_id': 0 } }
],
'as': "metalocator"
}}
])
实际上,这样做会更有效率,因为数据甚至不会返回到目标数组中,您也不需要重新映射"数组以丢弃其他字段。
为了记录,你错过了什么"是$first
运算符:
db.sample.aggregate([
{ "$lookup": {
"from": "locators",
"localField": "locator",
"foreignField": "_id",
"as": "metalocator"
}},
{ "$unwind": '$metalocator'},
{ "$group": {
"_id": "$_id",
"locator": { "$first": "$locator" },
"record": { "$first": "$record" },
"metalocator": {
"$push": {
"section": "$metalocator.section",
"cabinet": "$metalocator.cabinet"
}
}
}}
])