rmdir.cn
命令速查/MongoDB
🍃

MongoDB

MongoDB 数据库命令

CRUD 操作

13

查询文档

db.collection.find(<query>, <projection>)

查询单个文档

db.collection.findOne(<query>, <projection>)

插入单个文档

db.collection.insertOne(<document>)

批量插入文档

db.collection.insertMany([<doc1>, <doc2>, ...], <options>)

更新单个文档

db.collection.updateOne(<filter>, <update>, <options>)

批量更新文档

db.collection.updateMany(<filter>, <update>, <options>)

删除单个文档

db.collection.deleteOne(<filter>)

批量删除文档

db.collection.deleteMany(<filter>)

原子更新并返回文档

db.collection.findOneAndUpdate(<filter>, <update>, <options>)

删除并返回文档

db.collection.findOneAndDelete(<filter>, <options>)

批量写入操作

db.collection.bulkWrite([<op1>, <op2>, ...], <options>)

替换整条文档

db.collection.replaceOne(<filter>, <replacement>, <options>)

原子查找并替换文档

db.collection.findOneAndReplace(<filter>, <replacement>, <options>)

聚合管道

2

聚合管道查询

db.collection.aggregate([<stage1>, <stage2>, ...])

聚合管道高级阶段速查

db.collection.aggregate([{ <stage>: { ... } }, ...])

索引管理

6

创建索引

db.collection.createIndex(<keys>, <options>)

查看索引列表

db.collection.getIndexes()

删除索引

db.collection.dropIndex(<index>)

$text 文本索引全文搜索

db.collection.find({ $text: { $search: "<keywords>" } }, { score: { $meta: "textScore" } }).sort({ score: { $meta: "textScore" } })

创建自动过期数据的TTL索引

db.collection.createIndex({ <dateField>: 1 }, { expireAfterSeconds: <seconds> })

地理空间索引与位置查询

db.collection.createIndex({ <locationField>: "2dsphere" })

统计与计数

3

统计文档数量

db.collection.countDocuments(<filter>)

获取字段去重值

db.collection.distinct(<field>, <query>)

快速估算文档总数

db.collection.estimatedDocumentCount()

数据库与集合管理

8

删除集合

db.collection.drop()

查看数据库统计信息

db.stats()

列出所有数据库

show dbs

列出所有集合

show collections

切换数据库

use <database_name>

重命名集合

db.collection.renameCollection(<newName>, <dropTarget>)

显式创建集合

db.createCollection(<name>, <options>)

删除整个数据库

db.dropDatabase()

数据导入导出

4

导出数据库备份

mongodump [选项]

导入备份数据

mongorestore <path> [选项]

导出 JSON/CSV 数据

mongoexport --collection=<name> [选项]

导入 JSON/CSV 数据

mongoimport --collection=<name> --file=<path> [选项]

查询运算符

4

常用查询操作符速查

{ <field>: { <operator>: <value> } }

逻辑与条件组合操作符

{ <operator>: [ { condition1 }, { condition2 }, ... ] }

数组字段更新操作

{ $operator: { <field>: <value> } }

常用字段更新操作符速查

{ $operator: { <field1>: <value1>, ... } }

运维与监控

10

查询执行计划分析

db.collection.find(<query>).explain(<verbosity>)

慢查询分析与性能诊断

db.setProfilingLevel(<level>, <slowms>)

查看当前运行的操作

db.currentOp(<options>)

验证集合数据完整性

db.collection.validate(<options>)

副本集管理与状态查看

rs.<command>()

查看服务器运行状态

db.serverStatus()

游标(Cursor)迭代与处理方法

db.collection.find(<query>).<cursorMethod>()

实时服务器监控工具

mongostat [选项]

集合级别读写耗时监控

mongotop [选项]

终止正在运行的操作

db.killOp(<opid>)

变更流

1

监听数据变更 (Change Streams)

db.collection.watch(<pipeline>, <options>)

用户与权限

5

创建数据库用户

db.createUser({ user, pwd, roles, ... })

授予用户角色

db.grantRolesToUser("<username>", [<roles>])

删除数据库用户

db.dropUser("<username>")

创建自定义角色

db.createRole({ role: "<name>", privileges: [...], roles: [...] })

修改用户密码

db.changeUserPassword("<username>", "<newPassword>")

连接与配置

1

连接字符串格式及选项

mongodb://[username:password@]host[:port][/database][?options]

管理命令

1

执行数据库管理命令

db.runCommand({ <command>: <value>, ... })

事务

1

ACID 多文档事务操作

session.startTransaction(); ... session.commitTransaction() / session.abortTransaction()

分片

1

分片集群配置与状态查看

sh.<command>()

排序规则

1

国际化排序与比较规则

{ collation: { locale: "<lang>", ... } }