Mongodb aggregate commonly used statements

 

Comparison of commonly used aggregation statements and sql statements in Mongodb:

       

  1. $match is equivalent to where
  2. $group is equivalent to group by
  3. $match is equivalent to having
  4. $project is equivalent to select
  5. $sort is equivalent to order by
  6. $limit is equivalent to limit
  7. $sum is equivalent to sum()
  8. $count() is equivalent to count()
  9. Join is equivalent to $lookup
  10. The match before the group is to query the source data, and the match after the group is to filter the data after the group
  11. $in: Query multiple data, that is, a field element is in a list
  12. $nin: Query multiple data, that is, a certain field element is not in the same list
  13. $ne: not equal
  14. $gt/$gte: greater than/greater than or equal to
  15. $lt/$lte: less than/less than or equal to
  16. $set: add a new field, or update a field

Mongodb aggregate query, examples:

      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

 

 

 

 

     

 

Guess you like

Origin blog.csdn.net/xxy_yang/article/details/82702737