Mongodb的aggregate常用语句

Mongodb常用的聚合语句和sql语句的对比:

       

  1. $match相当于where
  2. $group 相当于 group by
  3. $match 相当于having
  4. $project 相当于 select
  5. $sort 相当于 order by
  6. $limit 相当于 limit
  7. $sum 相当于 sum()
  8. $count() 相当于 count()
  9. Join 相当于 $lookup
  10. group之前的match,是对源数据进行查询,group之后的match是对group之后的数据进行筛选
  11. $in:查询多个数据,就是某个字段元素在一个列表中
  12. $nin: 查询多个数据,就是某个字段元素不在一个列表中
  13. $ne:不相等
  14. $gt/$gte:大于/大于等于
  15. $lt/$lte:小于/小于等于
  16. $set:添加一个新的字段,或者更新一个字段

mongodb聚合查询,事例:

      db.table.aggregate([{$match:{“status”:1}},

           {$group:{_id:”$cust_id”,total:{$sum:”$price”}}}])

    解析:sql: select cust_id,sum(price) as total from table where status=1 group by cust_id

 

     

猜你喜欢

转载自blog.csdn.net/xxy_yang/article/details/82702737