因为 mongoDB 里和 aggregate 实在是太强大了,导致他有一堆指令。。。。这个文章主要为了记录我在使用 MongoDB 是否遇到的各种各样命令。
指令基本格式:
db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION) |
它类似这样的命令格式:
db.orders.aggregate([ | |
{ $match: { status: "A" } }, | |
{ $group: { _id: "$cust_id", total: { $sum: "$amount" } } }, | |
{ $sort: { total: -1 } } | |
], | |
{ explain: true } | |
) |
# count
mongoDB5.0 有 count 命令,但是但 mongoDB4.4 中是没有 count 命令的。
count 等价于 sum:1
db.col.aggregate([{ "$group":{"_id":"$title", "num":{"$sum":1 } } } ] ) |
db.col.aggregate([ | |
{ | |
"$group":{ | |
"_id":null, | |
"cnt":{"$sum":1} | |
} | |
} | |
]) |
下面这条命令用于查询集合中文档总数
# unset
{ $unset: "<field>" }
To remove a single field, the
$unsettakes a string that specifies the field to remove:
删除字段。
# group
group 命令,就是制定 _id 和 聚合函数 。
{ | |
$group: | |
{ | |
_id: <expression>, // Group By Expression | |
<field1>: { <accumulator1> : <expression1> }, | |
... | |
} | |
} |
# 常用聚合函数

# 常用管道函数
