Comparison of commonly used aggregation statements and sql statements in Mongodb:
- $match is equivalent to where
- $group is equivalent to group by
- $match is equivalent to having
- $project is equivalent to select
- $sort is equivalent to order by
- $limit is equivalent to limit
- $sum is equivalent to sum()
- $count() is equivalent to count()
- Join is equivalent to $lookup
- 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
- $in: Query multiple data, that is, a field element is in a list
- $nin: Query multiple data, that is, a certain field element is not in the same list
- $ne: not equal
- $gt/$gte: greater than/greater than or equal to
- $lt/$lte: less than/less than or equal to
- $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