node.js - 选择字段值作为数组 mongoose/mongodb 聚合

标签 node.js mongodb mongoose mongodb-query aggregation-framework

我有一个 mongo 查询

db.memberships.aggregate([{
        '$match': {
            unit: new ObjectId('566fbaa1e63225b10bacac44')
        }
    },

    {
        '$lookup': {
            from: 'seasons',
            localField: 'season',
            foreignField: '_id',
            as: 'season'
        }
    },

    {
        '$unwind': '$season'
    },
    {
        '$project': {
            season: {
                'name': '$season.name',
                'id': '$season._id'
            }
        }
    },
    {
        '$group': {
            _id: '$_id',
            'season': {
                '$addToSet': '$season'
            }
        }
    },

]);

结果

[
    {
        "_id": ObjectId("56a0e5a860d8a6e41eda2ea3"),
        "season": [{
            "name": "Summer",
            "id": ObjectId("56a0d6c692c26b8c019a2758")
        }]
    },
    {
        "_id": ObjectId("56a0e56d60d8a6e41eda2e9e"),
        "season": [{
            "name": "Summer",
            "id": ObjectId("56a0d6c692c26b8c019a2758")
        }]
    },
    {
        "_id": ObjectId("56a0e53860d8a6e41eda2e9a"),
        "season": [{
            "name": "Summer",
            "id": ObjectId("56a0d6c692c26b8c019a2758")
        }]
    },

    {
        "_id": ObjectId("56a0e4d660d8a6e41eda2e94"),
        "season": [{
            "name": "Winter",
            "id": ObjectId("5680dc01ba1e41f5066526fa")
        }]
    }
]

我需要获得这种格式的结果

{
    _id: [
        // ids here
    ],
    seasons: [{
            id: '56a0d6c692c26b8c019a2758',
            'name': 'Summer'
        },
        {
            id: '5680dc01ba1e41f5066526fa',
            'name': 'Winter'
        }
    ]
}

查询必须返回具有 2 个属性 idseason 的单个文档。季节必须是一系列独特的季节。每个季节都应该包含一个 id 和一个名称。感谢您的帮助!

最佳答案

使用 $group 重组您的管道 步骤将 _id 值为 null 来计算所有输入文档作为一个整体的累积值,从而将 id 字段创建为所有唯一 id 的数组:

var pipeline = [ 
    { 
        '$match': { 
            unit: new ObjectId('566fbaa1e63225b10bacac44')
        } 
    },

    { 
        '$lookup': { 
            from: 'seasons', localField: 'season', foreignField: '_id', as: 'season' 
        } 
    },
    {
        '$unwind' : '$season'
    },
    {
        '$project': {
            season:  {'name': '$season.name', 'id' : '$season._id'}
        }
    },
        { 
        '$group': {
            "_id": null,  
            "id": { "$addToSet": "$_id" },
            'season': { 
                '$addToSet': '$season' 
            }
        } 
    }       
];
db.memberships.aggregate(pipeline);

关于node.js - 选择字段值作为数组 mongoose/mongodb 聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34944923/

相关文章:

node.js - nodejs/winston logging——我想要几个日志文件,一个包含所有请求,一个只有 POST,另一个 GET(里面有详细信息)

node.js - 如何使用 Gulp 提供串联的 Javascript 文件?

javascript - 使用 Node 管理 mongoDB 连接的正确方法是什么?

angularjs - 通过从 Mongoose 获取数据连接的 angularjs 自动完成

node.js - 尚未声明 `distinct` 的值

MongoDB按ObjectIds数组聚合匹配

node.js - 在嵌套对象数组中过滤 geo_point

node.js - 如何在 Coffeescript 1.9 上强制使用生成器?

javascript - 在一个查询中为每个属性分组不同的值和计数

node.js - Mongoose 独特的 : true not work