码迷,mamicode.com
首页 > 数据库 > 详细

MongoDB Aggregate

时间:2014-10-08 04:49:34      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   sp   div   c   on   cti   log   

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 }

 

MongoDB Aggregate

标签:style   blog   color   sp   div   c   on   cti   log   

原文地址:http://www.cnblogs.com/phoenix13suns/p/4010366.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!