一、Aggregation-聚合查询和mysql sql语句对应
Aggregation:
参数说明:sql(Operators)
where ($match) 、group by ($group) 、having($match)、select($project)、order by($sort)、limit($limit)
sum($sum)、count($sum)、join($lookup)
SELECT cust_id, SUM(price) as total
FROM orders
WHERE status = 'A'
GROUP BY cust_id
HAVING total > 250
db.orders.aggregate([
{$match: {status: 'A'}},
{$group: {_id: "$cust_id",total: { $sum: "$price"}}},
{$match: {total: { $gt: 250}}}
])
二、更加字段长度排序
db.collection.aggregate( [ {$project: { "field": 1, "field_length": { $strLenCP: "$field" } }}, {$sort: {"field_length": -1}}, {$project: {"field_length": 0}}, ] )