1. sum
{ "_id" : ObjectId("50b1aa983b3d0043b51b2c52"), "name" : "Nexus 7", "category" : "Tablets", "manufacturer" : "Google", "price" : 199 }
select manufacturer,count(*) from xx
group by manufacturer
db.products.aggregate([ {$group: { _id:"$manufacturer", num_products:{$sum:1} } } ])
result
{ "_id" : "Amazon", "num_products" : 2 } { "_id" : "Sony", "num_products" : 1 } { "_id" : "Samsung", "num_products" : 2 { "_id" : "Google", "num_products" : 1 } { "_id" : "Apple", "num_products" : 4 }
时间: 2024-10-24 09:49:27