Mongo按对象键分组,但保留整个对象/字典

时间:2018-07-25 09:00:29

标签: mongodb group-by pymongo

我正在使用mongodb并尝试构建一个groupby查询。一个文档看起来像这样

{
    "_id" : "ca5ff110697611e89c2d9a0012d6e110",
    "CreatedAt" : ISODate("2018-06-06T16:15:55.000Z,
    "CreatedBy" : {
        "Email" : "neo@orangescape.com",
        "Name" : "Neo",
        "_id" : "User001"
    }, 
    },
    "Salary" : 200000
    "Branch" : "C2"
}

我的groupby查询看起来像这样

[{'$project': {'Branch': 1, 'CreatedAt.year':{$year:"$CreatedAt"}, 'CreatedBy': 1, 'Salary': 1}}, 
{'$group': {'_id': {'Branch': '$Branch', 
                    'Salary': '$Salary', 
                    'CreatedAt.year': "$CreatedAt.year" , 
                    'CreatedBy': '$CreatedBy._id'}, 
            'Salary': {'$sum': '$Salary'},
            'CreatedBy': "$CreatedBy"
            }}, 
           ]

如您所见,CreatedBy是一个嵌套字段,在其_id字段上分组仅产生_id字段,但我想保留name和{{1 }}。按整个email进行分组,因为用户可能会在某个时候更改其名称,而旧文档将具有旧名称。

1 个答案:

答案 0 :(得分:1)

$group阶段的每个字段都需要一个累加器运算符。要保留CreatedBy字段,可以使用$first例如:

新查询:

db.collection.aggregate([
  {
    "$project": {
      "Branch": 1,
      "CreatedAt.year": {
        $year: "$CreatedAt"
      },
      "CreatedBy": 1,
      "Salary": 1
    }
  },
  {
    "$group": {
      "_id": {
        "Branch": "$Branch",
        "Salary": "$Salary",
        "CreatedAtYear": "$CreatedAt.year",
        "CreatedBy": "$CreatedBy._id"
      },
      "Salary": {
        "$sum": "$Salary"
      },
      "CreatedBy": {
        "$first": "$CreatedBy"
      }
    }
  }
])

结果:

[
  {
    "CreatedBy": {
      "Email": "neo@orangescape.com",
      "Name": "Neo",
      "_id": "User001"
    },
    "Salary": 200000,
    "_id": {
      "Branch": "C2",
      "CreatedAtYear": 2018,
      "CreatedBy": "User001",
      "Salary": 200000
    }
  }
]

您可以在此处在线尝试:mongoplayground.net/p/cVNHRD5Rv21