仅使用$ lookup聚合中的有限键

时间:2017-07-14 06:14:38

标签: mongodb mongodb-query aggregation-framework

我想在$ lookup聚合后过滤数据。

现在,我想从子集合中获取唯一的location key值,而不是整个文档。如果我需要位置中的特定密钥,即zone_id我该怎么办?请帮忙。

我正在使用以下查询

//查询

db.parent.aggregate([
        {
          $lookup:
        {
          from: "child",
          localField: "pid",
          foreignField: "pid",
          as: "more"
        } 
       }
])

//数据 //儿童集合

 db.child.insert({
  "pid": 1,
  "name": "Max",
  "title": "Top" 
  "status": 1,

  "description": {
    "destination": "jur bagh",
    "community": "abc"
  },
  "location:": {
    "zone_id": "north",
    "city": "jaipur",
    "latitude": "12.121212",
    "longitude": "21.414134"
  }, 
  "created_by": "user_id",
  "modified_by": "user",
  "created_at": "12:00",
  "updated_at": "13:00"
});

//父集合

 db.parent.insert({
  "pid": 1,
  "pname": "PQW",
  "rox": "Labs",
  "status": 1,
  "created_by": "smdcd",
  "modified_by": "pink",
  "created_at": "12:00",
  "updated_at": "13:00"
});

我想要这样的结果

db.parent.insert({
  "pid": 1,
  "pname": "PQW",
  "rox": "Labs",
  "status": 1,
  "created_by": "smdcd",
  "modified_by": "pink",
  "created_at": "12:00",
  "updated_at": "13:00"
"more" [
"location:": {
    "zone_id": "north",
    "city": "jaipur",
    "latitude": "12.121212",
    "longitude": "21.414134"
  }
]


});

2 个答案:

答案 0 :(得分:0)

使用$ project https://docs.mongodb.com/manual/reference/operator/aggregation/project/

,{$project:{
  pid:1,
  pname:1,
  rox:1,
  status:1,
  modified_by:1,
  created_by:1,
  created_at:1,
  updated_at:1,
  more.location:$more.location
}}
// 'name of the field':1 means you want to keep the 'name'.
//  more.location:$more.location means you passing down the data you need.

要过滤结果,您可以将$ match添加到聚合中 https://docs.mongodb.com/manual/reference/operator/aggregation/match/

,{$match:{
    more.location.zone_id:'north'
}}

如果您有多个位置作为数组但只想返回一个,您可能需要先解开数组:

,{$unwind:'$more.location'}

答案 1 :(得分:0)

您希望此处$arrayElemAt引用"单曲"来自$lookup的结果,并使用可用的$addFields或所有字段$project将新字段放入文档中:

db.parent.aggregate([
  { "$lookup": {
    "from": "child",
    "localField": "pid",
    "foreignField": "pid",
    "as": "location"  
  }},
  { "$addFields": {
      "location": { "$arrayElemAt": [ "$location.location:", 0 ] }
  }}
])

另请注意,您的孩子的字段名称中似乎有一个拼写错误,因为它的名称为"location:",末尾带有冒号:。或者也许这只是一个错误。

产地:

{
    "_id" : ObjectId("5968821f7dcd6a5f6a9b4b7d"),
    "pid" : 1.0,
    "pname" : "PQW",
    "rox" : "Labs",
    "status" : 1.0,
    "created_by" : "smdcd",
    "modified_by" : "pink",
    "created_at" : "12:00",
    "updated_at" : "13:00",
    "location" : {
        "zone_id" : "north",
        "city" : "jaipur",
        "latitude" : "12.121212",
        "longitude" : "21.414134"
    }
}

根据您提问中提供的数据。

如果您打算使用多个结果,请使用$map进行处理:

db.parent.aggregate([
  { "$lookup": {
    "from": "child",
    "localField": "pid",
    "foreignField": "pid",
    "as": "more"  
  }},
  { "$addFields": {
    "more": {
      "$map": {
        "input": "$more.location:",
        "as": "l",
        "in": { "location": "$$l" }
      }
    }
  }}
])

结果如:

{
    "_id" : ObjectId("5968821f7dcd6a5f6a9b4b7d"),
    "pid" : 1.0,
    "pname" : "PQW",
    "rox" : "Labs",
    "status" : 1.0,
    "created_by" : "smdcd",
    "modified_by" : "pink",
    "created_at" : "12:00",
    "updated_at" : "13:00",
    "more" : [ 
        {
            "location" : {
                "zone_id" : "north",
                "city" : "jaipur",
                "latitude" : "12.121212",
                "longitude" : "21.414134"
            }
        }
    ]
}