计算$group MongoDB返回的记录

计算$group MongoDB返回的记录,mongodb,count,mongodb-query,aggregation-framework,Mongodb,Count,Mongodb Query,Aggregation Framework,如何编写返回下一次查询返回的项目计数的查询: db.books.aggregate( [ { $group : { _id : "$author", name: { $push: "$name" } } } ] ) 通过使用累加器运算符,此管道将为您提供每个给定作者的图书数量: db.books.aggregate([ { $group: { _id: "$author", name:

如何编写返回下一次查询返回的项目计数的查询:

db.books.aggregate(
    [
        { $group : { _id : "$author", name: { $push: "$name" } } }
    ]
)

通过使用累加器运算符,此管道将为您提供每个给定作者的图书数量:

db.books.aggregate([
    { 
        $group: {
            _id: "$author", 
            name: { $push: "$name" },
            count: { $sum: 1 } 
        } 
    }
])