如何使用 MongoDBs 聚合“ $lookup”作为“ findOne()”

大家都知道,find()返回一个结果数组,而 findOne()只返回一个简单的对象。

使用角度,这会产生很大的不同。我可以只写 {{myresult.name}}而不是 {{myresult[0].name}}

我发现聚合管道中的 $lookup方法返回一个结果数组,而不仅仅是一个对象。

例如,我有两个系列:

users收藏:

[{
"firstName": "John",
"lastName": "Smith",
"country": 123
}, {
"firstName": "Luke",
"lastName": "Jones",
"country": 321
}]

countries收藏:

[{
"name": "Australia",
"code": "AU",
"_id": 123
}, {
"name": "New Zealand",
"code": "NZ",
"_id": 321
}]

我的合计 $lookup:

db.users.aggregate([{
$project: {
"fullName": {
$concat: ["$firstName", " ", "$lastName"]
},
"country": "$country"
}
}, {
$lookup: {
from: "countries",
localField: "country",
foreignField: "_id",
as: "country"
}
}])

查询结果:

[{
"fullName": "John Smith",
"country": [{
"name": "Australia",
"code": "AU",
"_id": 123
}]
}, {
"fullName": "Luke Jones",
"country": [{
"name": "New Zealand",
"code": "NZ",
"_id": 321
}]
}]

从上面的结果可以看出,每个 country都是一个数组,而不是像 "country": {....}那样的单个对象。

我怎样才能让我的 $lookup返回一个单一的对象而不是一个数组,因为它将永远只匹配一个单一的文档?

52180 次浏览

差不多了,需要向管道添加另一个 $project stage,并使用 $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 ] }
}}
]
)

也可以使用 "preserveNullAndEmptyArrays"

像这样:

 db.users.aggregate(
[
{   "$project": {
"fullName": {
"$concat": [ "$firstName", " ", "$lastName"]
},
"country": "$country"
}},
{ "$lookup": {
"from": "countries",
"localField": "country",
"foreignField": "_id",
"as": "countryInfo"
}},
{"$unwind": {
"path": "$countryInfo",
"preserveNullAndEmptyArrays": true
}
},
]
)

如果不想重复 project 中的所有字段,只需用 $addFields 覆盖有问题的字段:

db.users.aggregate([
{   "$project": {
"fullName": {
"$concat": [ "$firstName", " ", "$lastName"]
},
"country": "$country"
}},
{ "$lookup": {
"from": "countries",
"localField": "country",
"foreignField": "_id",
"as": "countryInfo"
}},
{ "$addFields": {
"countryInfo": {
"$arrayElemAt": [ "$countryInfo", 0 ]
}
}}
])
db.users.aggregate([
{
$lookup: {
from: 'countries',
localField: 'country',
foreignField: '_id',
as: 'country'
}
},
{
$unwind: '$country'
}
]).pretty()

您可以使用这个 mongo 查询来获取 country 对象

我认为最干净的方法是在管道上再添加一个步骤:

...,
{
$addFields : {
$country: { $first: '$country' }
}
}