MongoDB-需要帮助进行一些聚合

MongoDB-需要帮助进行一些聚合,mongodb,aggregate,Mongodb,Aggregate,我尝试在MongoDB中进行聚合时遇到了麻烦 我需要交叉每个用户的一些信息,作为最终结果,我想要一个用户列表,其中每个用户只有一个对象,每个对象都有一些具有不同信息的列表 1-createdAtList数组必须从最早的日期到最新的日期排序。总金额是指当前位置的总金额与下面代码中所示的前一个总金额相加,而不仅仅是总金额的总和 2-类别列表的顺序必须如下所示:类别1、类别2、类别3 3-所需的最终结果必须按如下顺序排列:user1、user2、user3 基本上,我需要一些帮助来完成以下工作: //

我尝试在MongoDB中进行聚合时遇到了麻烦

我需要交叉每个用户的一些信息,作为最终结果,我想要一个用户列表,其中每个用户只有一个对象,每个对象都有一些具有不同信息的列表

1-createdAtList数组必须从最早的日期到最新的日期排序。总金额是指当前位置的总金额与下面代码中所示的前一个总金额相加,而不仅仅是总金额的总和

2-类别列表的顺序必须如下所示:类别1、类别2、类别3

3-所需的最终结果必须按如下顺序排列:user1、user2、user3

基本上,我需要一些帮助来完成以下工作:

//List of docs from my collection:
[
    {
        _id: "doc1",
        user: "user1",
        category: "category1",
        createdAt: "2018-01-01T00:00:00.000Z"
    },
    {
        _id: "doc2",
        user: "user1",
        category: "category2",
        createdAt: "2017-12-12T00:00:00.000Z",
    },
    {
        _id: "doc3",
        user: "user1",
        category: "category1",
        createdAt: "2017-12-12T00:00:00.000Z",
    },
    {
        _id: "doc4",
        user: "user1",
        category: "category2",
        createdAt: "2018-01-01T00:00:00.000Z"
    },
    {
        _id: "doc5",
        user: "user1",
        category: "category3",
        createdAt: "2017-11-11T00:00:00.000Z"
    }
]

//Desired result:
{
    user: "user1",
    createdAtList: [ //list ordered by createdAt
        {
            createdAt: "2017-11-11T00:00:00.000Z",
            total: 1,
            sumOfTotal: 0
        }
        {
            createdAt: "2017-12-12T00:00:00.000Z",
            total: 2,
            sumOfTotal: 3 //summed up with the previous
        }
        {
            createdAt: "2018-01-01T00:00:00.000Z",
            total: 2,
            sumOfTotal: 5 //summed up with the previous
        }
    ],
    categotyList: [ //list ordered by category
        {
            category: "category1",
            total: 2
        },
        {
            category: "category2",
            total: 2
        },
        {
            category: "category3",
            total: 1
        }
    ]
},
...

是否可以在同一个聚合中执行此操作?

我认为使用createdAtList.sumOfTotal字段没有意义。我不认为数组中的字段应该依赖于元素的特定顺序。如果希望某个字段包含createdAtList.total字段的总和,我认为数组外应该只有一个字段。也就是说,下面是我提出的查询,它使用用户作为集合的名称,为您提供所需的结果:

db.users.aggregate([
    {
        $group: {
            _id: {
                user: "$user",
                createdAt: "$createdAt"
            },
            total: { $sum: 1 },
            category: { $push: "$category" }
        }
    },
    {
        $project: {
            _id: 0,
            user: "$_id.user",
            createdAt: "$_id.createdAt",
            total: "$total",
            category: 1
        }
    },
    { $unwind: "$category" },
    {
        $group: {
            _id: {
                user: "$user",
                category: "$category"
            },
            catTotal: { $sum: 1 },
            createdAtList: {
                $push: {
                    createdAt: "$createdAt",
                    total: "$total"
                }
            }
        }
    },
    {
        $project: {
            _id: 0,
            user: "$_id.user",
            createdAtList: 1,
            category: "$_id.category",
            catTotal: 1
        }
    },
    { $unwind: "$createdAtList" },
    {
        $group: {
            _id: "$user",
            createdAtList: {
                $addToSet: "$createdAtList"
            },
            categoryList: {
                $addToSet: {
                    category: "$category",
                    total: "$catTotal"
                }
            }
        }
    },
    { $unwind: "$createdAtList" },
    { $sort: { "createdAtList.createdAt": 1 } },
    {
        $group: {
            _id: "$_id",
            createdAtList: {
                $push: "$createdAtList"
            },
            categoryList: {
                $first: "$categoryList"
            }
        }
    },
    { $unwind: "$categoryList" },
    { $sort: { "categoryList.category": 1 } },
    {
        $group: {
            _id: "$_id",
            createdAtList: {
                $first: "$createdAtList"
            },
            categoryList: {
                $push: "$categoryList"
            }
        }
    },
    {
        $project: {
        _id: 0,
        user: "$_id",
        createdAtList: 1,
        sumOfTotal: { $sum: "$createdAtList.total" },
        categoryList: 1
        }
    },
    { $sort: { user: 1 } },
]).pretty()

嗨,谢谢你的帮助。你的解决方案很有效。然而,我编辑了我的问题,试图更好地解释我试图用sumOfTotal做什么。此外,必须对createdAtList、categoryList和最终结果进行排序。请看编辑。我理解你试图用sumOfTotal做什么,但我仍然认为这样做不是一个好的设计。通常,数组应该与顺序无关。我已经对categoryId进行了排序,但我将计划编辑其他排序,并尝试按照指定的方式进行汇总。我现在不在家,所以你说的对,别担心。我可以在JS上轻松地完成这项工作,并对createdAtList进行排序。再次感谢。我按照要求编辑了查询。除非我疯了,否则您将userId字段和categoryId字段的名称分别更改为user和category。我还更新了我的答案以反映这一变化。嗨。只是给你一个反馈。它工作得很好。谢谢你帮助我,你没有失去你的心,我已经改变了属性名称,哈哈哈。