MongoDb:如何对日期字段进行汇总,分组和排序?

Mar*_*coS 2 mongodb mongodb-query aggregation-framework

在我的MongoDB人员集合中,我需要过滤具有相同“别名”属性值的人员,保留其中第一个属性,还要使所有人员都具有无效的“别名”。

数据是这样的:

{ "_id" : "1", "flag" : true,  "name" : "Alice",    "alias" : null, "dateOfBirth": new ISODate('1995-12-27T00:00:00.000Z') },
{ "_id" : "2", "flag" : true,  "name" : "Bob",      "alias" : "4c", "dateOfBirth": new ISODate('1996-12-27T00:00:00.000Z') },
{ "_id" : "3", "flag" : true,  "name" : "Bobby",    "alias" : "4c", "dateOfBirth": new ISODate('1997-12-27T00:00:00.000Z') },
{ "_id" : "4", "flag" : true,  "name" : "Cristina", "alias" : null, "dateOfBirth": new ISODate('1998-12-27T00:00:00.000Z') },
{ "_id" : "5", "flag" : false, "name" : "Diego",    "alias" : null, "dateOfBirth": new ISODate('1999-12-27T00:00:00.000Z') },
{ "_id" : "6", "flag" : true,  "name" : "Zoe",      "alias" : "22", "dateOfBirth": new ISODate('2000-12-27T00:00:00.000Z') }
Run Code Online (Sandbox Code Playgroud)

这是我的查询:

{ "_id" : "1", "flag" : true,  "name" : "Alice",    "alias" : null, "dateOfBirth": new ISODate('1995-12-27T00:00:00.000Z') },
{ "_id" : "2", "flag" : true,  "name" : "Bob",      "alias" : "4c", "dateOfBirth": new ISODate('1996-12-27T00:00:00.000Z') },
{ "_id" : "3", "flag" : true,  "name" : "Bobby",    "alias" : "4c", "dateOfBirth": new ISODate('1997-12-27T00:00:00.000Z') },
{ "_id" : "4", "flag" : true,  "name" : "Cristina", "alias" : null, "dateOfBirth": new ISODate('1998-12-27T00:00:00.000Z') },
{ "_id" : "5", "flag" : false, "name" : "Diego",    "alias" : null, "dateOfBirth": new ISODate('1999-12-27T00:00:00.000Z') },
{ "_id" : "6", "flag" : true,  "name" : "Zoe",      "alias" : "22", "dateOfBirth": new ISODate('2000-12-27T00:00:00.000Z') }
Run Code Online (Sandbox Code Playgroud)

哪个返回:

db.people.aggregate([ 
    { '$match': { 'flag': true } }, 
    { '$project': {
        'name': 1,          
        'alias': { 
            '$cond': [
                { '$eq': [ '$alias', null ] }, 
                '$_id', 
                '$alias' 
            ]
        }
    }},
    { '$group': {
        '_id': '$alias',         
        'name':  { '$first': '$name' },          
        'id': { '$first': '$_id' }       
    }}, 
    { '$project': {
        'alias': {
            '$cond': [ 
                { '$eq': [ '$id', '$_id' ] }, 
                null, 
               '$_id' 
            ]
        }, 
        'name': 1,
        '_id': '$id'
    }}
])
Run Code Online (Sandbox Code Playgroud)

到目前为止,一切都很好。

更新:至此,问题类似于建议的重复问题。现在是不同的部分:

我需要根据“ dateOfBirth”字段对其进行排序。
非常有信心,我将查询更改为:

{ "_id" : "6", "name" : "Zoe",      "alias" : "22" }
{ "_id" : "4", "name" : "Cristina", "alias" : null }
{ "_id" : "2", "name" : "Bob",      "alias" : "4c" }
{ "_id" : "1", "name" : "Alice",    "alias" : null }
Run Code Online (Sandbox Code Playgroud)

但这给出了:

db.people.aggregate([ 
    { '$match': { 'flag': true } }, 
    { '$project': {
        'name': 1,          
        'dateOfBirth': 1,
        'alias': { 
            '$cond': [
                { '$eq': [ '$alias', null ] }, 
                '$_id', 
                '$alias' 
            ]
        }
    }},
    { '$group': {
        '_id': '$alias',         
        'name':  { '$first': '$name' },          
        'dateOfBirth': { '$first': '$dateOfBirth' },
        'id': { '$first': '$_id' }       
    }}, 
    { '$project': {
        'alias': {
            '$cond': [ 
                { '$eq': [ '$id', '$_id' ] }, 
                null, 
               '$_id' 
            ]
        }, 
        'name': 1,
        '_id': '$id',
        'dateOfBirth': 1,
    }},
    { '$sort': { 'dateOfBirth': 1 }}
])
Run Code Online (Sandbox Code Playgroud)

当然,这是错的:dateOfBirth领域没有通过$group阶段...

关于如何通过的任何线索?

Bla*_*ven 7

实际上,这对我来说很好,并且我怀疑您正在运行的真实代码中有错别字(不是该清单),而在某处缺少了“ dateOfBirth”(或拼写错误)。

但是,如果这里要吸取教训,那就应该不要分头$project$group分阶段,因为引入另一个管道阶段不仅效率低下(这意味着额外的数据传递),而且在指定要包含的数据时通常会造成混淆在管线中。

所以宁愿:

db.people.aggregate([
    { "$match": { "flag": true } },
    { "$group": {
        "_id": {
            "$ifNull": [ "$alias", "$_id" ]
        },
        "name": { "$first": "$name" },
        "dateOfBirth": { "$first": "$dateOfBirth" },
        "id": { "$first": "$_id" }
    }},
    { "$project": {
        "_id": "$id",
        "name": 1,
        "dateOfBirth": 1,
        "alias": {
            "$cond": [
                { "$eq": [ "$_id", "$id" ] },
                null,
                "$_id"
            ]
        }
    }},
    { "$sort": { "dateOfBirth": 1 } }
]) 
Run Code Online (Sandbox Code Playgroud)

这也可以$ifNull用作自然测试,而不是在$cond不需要的地方使用。

当然,返回的结果是:

{ "_id" : "1", "name" : "Alice", "dateOfBirth" : ISODate("1995-12-27T00:00:00Z"), "alias" : null }
{ "_id" : "2", "name" : "Bob", "dateOfBirth" : ISODate("1996-12-27T00:00:00Z"), "alias" : "4c" }
{ "_id" : "4", "name" : "Cristina", "dateOfBirth" : ISODate("1998-12-27T00:00:00Z"), "alias" : null }
{ "_id" : "6", "name" : "Zoe", "dateOfBirth" : ISODate("2000-12-27T00:00:00Z"), "alias" : "22" }
Run Code Online (Sandbox Code Playgroud)

如果您想“首先按出生日期”,则将排序移到$group舞台之前,$first操作员将在那里进行所有工作。