MongoDB对象数组中的聚合字段

时间:2019-02-19 14:20:40

标签: mongodb aggregate

不幸的是,我正在尝试解决问题一段时间,但没有运气。 因此,我正在重构一些旧代码(使用了所有已知的获取每个doc查询并对其进行循环),并且我试图汇总结果以删除BE进行的数千次调用。

当前文档如下所示

{
    "_id" : ObjectId("5c176fc65f543200019f8d66"),
    "category" : "New client",
    "description" : "",
    "createdById" : ObjectId("5c0a858da9c0f000018382bb"),
    "createdAt" : ISODate("2018-12-17T09:43:34.642Z"),
    "sentAt" : ISODate("2018-12-17T09:44:25.902Z"),
    "scheduleToBeSentAt" : ISODate("2018-01-17T11:43:00.000Z"),
    "recipients" : [ 
        {
            "user" : ObjectId("5c0a858da9c0f000018382b5"),
            "status" : {
                "approved" : true,
                "lastUpdated" : ISODate("2018-01-17T11:43:00.000Z")
            }
        }, 
        {
            "user" : ObjectId("5c0a858da9c0f000018382b6"),
            "status" : {
                "approved" : true,
                "lastUpdated" : ISODate("2018-01-17T11:43:00.000Z")
            }
        }, 
    ],
    "recipientsGroup" : "All",
    "isActive" : false,
    "notificationSent" : true
}

字段recipients.userUsers集合中用户的objectID。 修改此内容的正确方法是什么?

{
    "_id": ObjectId("5c176fc65f543200019f8d66"),
    "category": "New client",
    "description": "",
    "createdById": ObjectId("5c0a858da9c0f000018382bb"),
    "createdAt": ISODate("2018-12-17T09:43:34.642Z"),
    "sentAt": ISODate("2018-12-17T09:44:25.902Z"),
    "scheduleToBeSentAt": ISODate("2018-01-17T11:43:00.000Z"),
    "recipients": [{
            "user": {
                "_id": ObjectId("5c0a858da9c0f000018382b5"),
                "title": "",
                "firstName": "Monique",
                "lastName": "Heinrich",
                "qualification": "Management",
                "isActive": true
            },
            "status": {
                "approved": true,
                "lastUpdated": ISODate("2018-01-17T11:43:00.000Z")
            }
        },
        {
            "user": {
                "_id": ObjectId("5c0a858da9c0f000018382b6"),
                "title": "",
                "firstName": "Marek",
                "lastName": "Pucelik",
                "qualification": "Management",
                "isActive": true
            },
            "status": {
                "approved": true,
                "lastUpdated": ISODate("2018-01-17T11:43:00.000Z")
            }
        },
    ],
    "recipientsGroup": "All",
    "isActive": false,
    "notificationSent": true
}

聚合是一种强大的工具,但有时简单的解决方案会使您的大脑受伤.....

我尝试了类似的方法,但是也没有运气。

db.getCollection('Protocols').aggregate([
{
    $lookup: {
        from: "Users",
        localField: "recipients.user",
        foreignField: "_id",
        as: "users"
    }
},
{
    $project: {
        "recipients": {
            "status": 1,
            "user": {
                $filter: {
                input: "$users",
                cond: { $eq: ["$$this._id", "$user"] }
                }
            },
        }
    }
}
])

1 个答案:

答案 0 :(得分:0)

您可以在聚合管道中使用$lookup运算符

https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/

但是出于性能原因,您宁愿在recipents数组中复制用户对象,以避免此类复杂的查询。