如何使用聚合查询在mongodb中进行计数?

问题描述 投票:0回答:1

我试图使用聚合查询来获取数据库的数量。

MY Scenrio

访问

[{
    "id": 1,
    name: 'laptop'
}, {
    "id": 2,
    name: 'deasktop'
}]

查询

db.access.aggregate([
    {
        $match: {}
    },
    {
        $group: {
            total: { $sum: 1 }
        }
    }
])

执行查询我得到了输出

[{
    "total": 2
}]

Excepted output

{
    total: 1
    result: [{
        "id": 1,
        name: 'laptop'
    }, {
        "id": 2,
        name: 'deasktop'
    }]
}

如何使用聚合查询获取此输出。

mongodb mongodb-query aggregation-framework
1个回答
0
投票
您需要在$push阶段中$group您的文档
© www.soinside.com 2019 - 2024. All rights reserved.