将嵌套文档投影到根级别,而无需使用$ project

时间:2018-12-06 16:33:55

标签: javascript mongodb mongodb-query aggregation-framework

使用聚合管道,我试图将嵌入式文档投影到根级别,而不必单独投影每个字段而无需替换根级别。

例如,我要将这个集合投影到根级别:

[
    {
        _id: "1",
        city: "NY"
        user: [ {
            firstName: "John",
            lastname: "Peters",
            brothers: [
                { _id: "B1",
                  brotherFirstName: "Karl" }
            ]
         }, {
            firstName: "Raul",
            lastname: "Other",
            brothers: [
                { _id: "B2",
                  brotherFirstName: "May" }
            ]
         }, {
            firstName: "Paul",
            lastname: "Ray",
            brothers: [
                { _id: "B3",
                  brotherFirstName: "Bryan" }
            ]
         }        
    },
    {
        _id: "2",
        city: "NY"
        user: [ {
            firstName: "Joe",
            lastname: "Anders",
            brothers: [
                { _id: "B4",
                  brotherFirstName: "Carla" }
            ]
         }, {
            firstName: "Zoy",
            lastname: "Bat",
            brothers: [
                { _id: "B5",
                  brotherFirstName: "Stuart" }
            ]
         }, {
            firstName: "Ana",
            lastname: "Lily",
            brothers: [
                { _id: "B6",
                  brotherFirstName: "Carter" }
            ]
         }        
    }
]

这就是我要寻找的:对于子文档数组中的每个文档,我都希望将其投影到根级别,因此对于每个“用户”我都希望将其投影到“新文档”以及我想要的每个“兄弟”将其投影到“新文档”

[{
  _id: "1",
  city: "NY"
  firstName: "John",
  lastname: "Peters",
  brotherFirstName: "Karl" 
}, { 
  _id: "1",
  city: "NY"
  firstName: "Raul",
  lastname: "Other",
  brotherFirstName: "May" 
}, {
  _id: "1",
  city: "NY"
  firstName: "Paul",
  lastname: "Ray",
  brotherFirstName: "Bryan" 
}, {
  _id: "2",
  city: "NY"
  firstName: "Joe",
  lastname: "Anders",
  brotherFirstName: "Carla"
 }, {
  _id: "2",
  city: "NY"
  firstName: "Zoy",
  lastname: "Bat",
  brotherFirstName: "Stuart"
 }, {
  _id: "2",
  city: "NY"
  firstName: "Ana",
  lastname: "Lily",
  brotherFirstName: "Carter"
 }        
]

我尝试汇总$ unwind和$ replaceRoot,但是我无法“替换根”,因为我需要这些字段

db.getCollection('myColl').aggregate([  { $unwind : "$users" },  {$replaceRoot: { newRoot: "$users" }}, {$unwind : "$brothers" } ])

编辑

运行@Anthony Winzlet查询,我得到以下输出:

[
  {
    "_id": "B1",
    "brotherFirstName": "Karl",
    "city": "NY",
    "firstName": "John",
    "lastname": "Peters"
  },
  {
    "_id": "B2",
    "brotherFirstName": "May",
    "city": "NY",
    "firstName": "Raul",
    "lastname": "Other"
  },
  {
    "_id": "B3",
    "brotherFirstName": "Bryan",
    "city": "NY",
    "firstName": "Paul",
    "lastname": "Ray"
  },
  {
    "_id": "B4",
    "brotherFirstName": "Carla",
    "city": "NY",
    "firstName": "Joe",
    "lastname": "Anders"
  },
  {
    "_id": "B5",
    "brotherFirstName": "Stuart",
    "city": "NY",
    "firstName": "Zoy",
    "lastname": "Bat"
  },
  {
    "_id": "B6",
    "brotherFirstName": "Carter",
    "city": "NY",
    "firstName": "Ana",
    "lastname": "Lily"
  }
]

字段_id被兄弟的_id字段覆盖。我需要从根开始的项目_id

1 个答案:

答案 0 :(得分:1)

您需要在{strong> $$ROOT 文档中使用$mergeObjects。首先使用user,其次使用brother,最后必须使用$project删除userbrother对象

db.collection.aggregate([
  { "$unwind": "$user" },
  { "$replaceRoot": {
    "newRoot": { "$mergeObjects": ["$$ROOT", "$user"] }
  }},
  { "$unwind": "$brothers" },
  { "$replaceRoot": {
    "newRoot": { "$mergeObjects": ["$brothers", "$$ROOT"] }
  }},
  { "$project": { "brothers": 0, "user": 0 }}
])

Output

[
  {
    "_id": "1",
    "brotherFirstName": "Karl",
    "city": "NY",
    "firstName": "John",
    "lastname": "Peters"
  },
  {
    "_id": "1",
    "brotherFirstName": "May",
    "city": "NY",
    "firstName": "Raul",
    "lastname": "Other"
  },
  {
    "_id": "1",
    "brotherFirstName": "Bryan",
    "city": "NY",
    "firstName": "Paul",
    "lastname": "Ray"
  },
  {
    "_id": "2",
    "brotherFirstName": "Carla",
    "city": "NY",
    "firstName": "Joe",
    "lastname": "Anders"
  },
  {
    "_id": "2",
    "brotherFirstName": "Stuart",
    "city": "NY",
    "firstName": "Zoy",
    "lastname": "Bat"
  },
  {
    "_id": "2",
    "brotherFirstName": "Carter",
    "city": "NY",
    "firstName": "Ana",
    "lastname": "Lily"
  }
]