【问题标题】:In MongoDB / mongoose sort and limit result of $push aggregate在 MongoDB / mongoose 中排序和限制 $push 聚合的结果
【发布时间】:2017-01-05 19:50:42
【问题描述】:

我想对 $group 和 $push 的结果进行排序和限制。

假设我有一个在线订单系统。我想得到一个列表,其中包含每年按年份和产品数量排序的 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

返回的对象应该是这样的:

[
    {
        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
            }
        ]
    },
    ...
]

在猫鼬中,我可以按年份对数据进行分组:

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

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

它会给我所有的产品名称和数量,而不是排序。

我不知道如何对分组数据进行排序和限制。 我试过 $sort, $limit, $each 都没有成功。

【问题讨论】:

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


    【解决方案1】:

    在分组之前对数据进行排序。然后最后你可以使用$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 ]}
                }
            }
    ]
    

    【讨论】:

      猜你喜欢
      • 2020-04-23
      • 2022-12-12
      • 2017-04-10
      • 2015-12-13
      • 2014-12-24
      • 2021-06-01
      • 2018-12-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多