如何使用 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:46:29  来源:igfitidea点击:

How to use MongoDBs aggregate `$lookup` as `findOne()`

mongodbmongodb-queryaggregation-framework

提问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 $lookupmethod in the aggregate pipeline returns an array of results instead of just a single object.

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

For example, I have two colletions:

例如,我有两个集合:

userscollection:

users收藏:

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

countriescollection:

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 countryis an array instead of a single object like "country": {....}.

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

How can I have my $lookupreturn 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 $projectstage to your pipeline and use the $arrayElemAtto 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
                }
            },
        ]
    )