如何使用 MongoDB 将 `$lookup` 聚合为 `findOne()`
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37691727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use MongoDBs aggregate `$lookup` as `findOne()`
提问by Fizzix
So as you all know, find()
returns an array of results, with findOne()
returning just a simply object.
众所周知,find()
返回一个结果数组,findOne()
只返回一个简单的对象。
With Angular, this makes a huge difference. Instead of going {{myresult[0].name}}
, I can simply just write {{myresult.name}}
.
使用 Angular,这会产生巨大的差异。而不是去{{myresult[0].name}}
,我可以简单地写{{myresult.name}}
。
I have found that the $lookup
method in the aggregate pipeline returns an array of results instead of just a single object.
我发现$lookup
聚合管道中的方法返回一个结果数组,而不仅仅是一个对象。
For example, I have two colletions:
例如,我有两个集合:
users
collection:
users
收藏:
[{
"firstName": "John",
"lastName": "Smith",
"country": 123
}, {
"firstName": "Luke",
"lastName": "Jones",
"country": 321
}]
countries
collection:
countries
收藏:
[{
"name": "Australia",
"code": "AU",
"_id": 123
}, {
"name": "New Zealand",
"code": "NZ",
"_id": 321
}]
My aggregate $lookup
:
我的聚合$lookup
:
db.users.aggregate([{
$project: {
"fullName": {
$concat: ["$firstName", " ", "$lastName"]
},
"country": "$country"
}
}, {
$lookup: {
from: "countries",
localField: "country",
foreignField: "_id",
as: "country"
}
}])
The results from the query:
查询结果:
[{
"fullName": "John Smith",
"country": [{
"name": "Australia",
"code": "AU",
"_id": 123
}]
}, {
"fullName": "Luke Jones",
"country": [{
"name": "New Zealand",
"code": "NZ",
"_id": 321
}]
}]
As you can see by the above results, each country
is an array instead of a single object like "country": {....}
.
从上面的结果可以看出,每个country
都是一个数组,而不是像"country": {....}
.
How can I have my $lookup
return a single object instead of an array since it will only ever match a single document?
我怎样才能让我$lookup
返回单个对象而不是数组,因为它只会匹配单个文档?
回答by styvane
You're almost there, you need to add another $project
stage to your pipeline and use the $arrayElemAt
to return the single element in the array.
您快完成了,您需要向$project
管道添加另一个阶段并使用$arrayElemAt
返回数组中的单个元素。
db.users.aggregate(
[
{ "$project": {
"fullName": {
"$concat": [ "$firstName", " ", "$lastName"]
},
"country": "$country"
}},
{ "$lookup": {
"from": "countries",
"localField": "country",
"foreignField": "_id",
"as": "countryInfo"
}},
{ "$project": {
"fullName": 1,
"country": 1,
"countryInfo": { "$arrayElemAt": [ "$countryInfo", 0 ] }
}}
]
)
回答by Ros
You can also use "preserveNullAndEmptyArrays"
你也可以使用 "preserveNullAndEmptyArrays"
Like so:
像这样:
db.users.aggregate(
[
{ "$project": {
"fullName": {
"$concat": [ "$firstName", " ", "$lastName"]
},
"country": "$country"
}},
{ "$lookup": {
"from": "countries",
"localField": "country",
"foreignField": "_id",
"as": "countryInfo"
}},
{"$unwind": {
"path": "$countryInfo",
"preserveNullAndEmptyArrays": true
}
},
]
)