简体   繁体   English

在MongoDB /猫鼬中排序和限制$ push聚合的结果

[英]In MongoDB / mongoose sort and limit result of $push aggregate

I want to order and limit the results of and agregate, $group and $push. 我想排序和限制结果,并合计$ group和$ push。

Let's say that I have a system with online orders. 假设我有一个带有在线订单的系统。 And I want to get a list with 3 most ordered products each year sorted by year and products amount. 我想获得一个列表,其中包含每年订购最多的3种产品,按年份和产品数量排序。

year    product     amount

2016    product A   100
2016    product B   200
2016    product C   300
2016    product D   1000
2016    product E   50

2015    product F   100
2015    product G   800
2015    product A   300
2015    product B   400
2015    product C   100

2014    product A   70
2014    product B   50
2014    product G   500
2014    product C   600
2014    product D   900

The returned object should be something like this: 返回的对象应该是这样的:

[
    {
        year: 2016,
        products: [
            {
                product: 'product D',
                amount: 1000
            },
            {
                product: 'product C',
                amount: 300
            },
            {
                product: 'product B',
                amount: 200
            }
        ]
    },
    {
        year: 2015,
        products: [
            {
                product: 'product G',
                amount: 800
            },
            {
                product: 'product B',
                amount: 400
            },
            {
                product: 'product A',
                amount: 300
            }
        ]
    },
    ...
]

In mongoose I can group data by year: 在猫鼬中,我可以按年份分组数据:

Orders.model
    .aggregate([
        {
            $group: {
                _id: {
                    'year': '$year',
                },
                products: {
                    $push: {
                        product: '$product',
                        amount: '$amount'
                    }
                }
            }
        },

        {
            $project: {
                _id: 0,
                year: '$_id.year',
                products: '$products'
            }
        }
    ])

It will give me all product names and amounts and not sorted. 它会给我所有产品名称和数量,而不是排序。

By I don't know how to sort and limit the grouped data. 通过我不知道如何排序和限制分组的数据。 I tried $sort, $limit, $each without success. 我尝试$ sort,$ limit,$ each没有成功。

Sort your data before grouping. 分组之前对数据进行排序。 Then at the end you can use $slice to get the top three elements: 然后最后,您可以使用$slice获取前三个元素:

[
        {
            $sort: {
                year:-1,
                amount:-1
            }
        },
        {
            $group: {
                _id: {
                    'year': '$year',
                },
                products: {
                    $push: {
                        product: '$product',
                        amount: '$amount'
                    }
                }
            }
        },
        {
            $project: {
                _id: 0,
                year: '$_id.year',
                product: {$slice: [ "$products", 3 ]}
            }
        }
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM