对象数组中的MongoDB聚合字段 [英] MongoDB aggregate field in array of objects

查看:54
本文介绍了对象数组中的MongoDB聚合字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to solve a problem for some time now but with no luck, unfortunately. So I'm refactoring some old code (which used the all known get each doc query and for loop over it) and I'm trying to aggregate the results to remove the thousands of calls the BE is making.

当前文档看起来像这样

{
    "_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.user 是来自 Users 集合的用户的 objectID.修改它的正确方法是什么,结果将是

The field recipients.user is an objectID of a user from the Users collection. What is the correct way to modify this so the result will 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": {
                "_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
}

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

An aggregation is a powerful tool but sometimes the simple solution makes your brain hurt.....

我尝试过类似的事情,但也没有运气.

I tried something like this but with no luck also.

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"] }
                }
            },
        }
    }
}
])

推荐答案

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

You can use the $lookup operator in your aggregation pipeline

https://docs.mongodb.com/manual/reference/运算符/聚合/查找/

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

But for performance reason you'd rather duplicate user object in your recipents array to avoid such complex queries.

这篇关于对象数组中的MongoDB聚合字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆