文档菜单
文档首页
/
MongoDB Shell

mongosh 帮助

本页内容

  • 命令行帮助
  • mongosh Shell 帮助
  • 数据库帮助
  • 集合帮助
  • 游标帮助
  • BSON 类帮助
  • 命令助手

本文档提供了 mongosh 中可用的帮助概述mongosh.

提示

在访问 mongosh 中的帮助时,您可以使用 .help().help 语法。

要查看运行 mongosh 可执行文件和连接到部署的选项,请使用命令行中的 --help 选项

mongosh --help

要查看 mongosh 控制台中可用的命令列表,请在运行的 mongosh 控制台中输入 help

help

您可以在 mongosh 控制台中查看数据库级别的信息

默认情况下,mongosh 会显示提示符中的当前数据库。您也可以通过运行 db 命令来查看当前数据库

db

要查看服务器上可用的数据库列表,请使用 show dbs 命令

show dbs

show databasesshow dbs 的别名。

提示

数据库列表将根据您的访问权限而变化。有关查看数据库的访问限制的更多信息,请参阅listDatabases.

要查看您可以在 db 对象上使用的数据库方法列表,请运行 数据库方法,运行 db.help()

db.help()

输出类似于以下缩略列表

Database Class:
getMongo Returns the current database connection
getName Returns the name of the DB
getCollectionNames Returns an array containing the names of all collections in the current database.
getCollectionInfos Returns an array of documents with collection information, i.e. collection name and options, for the current database.
runCommand Runs an arbitrary command on the database.
adminCommand Runs an arbitrary command against the admin database.
...

mongosh 中查看特定数据库方法的帮助,请输入 db.<方法名>,然后输入 .help.help()。以下示例返回关于 db.adminCommand() 方法的帮助

db.adminCommand.help()

输出结果类似于以下内容

db.adminCommand({ serverStatus: 1 }):
Runs an arbitrary command against the admin database.
For more information on usage: https://mongodb.ac.cn/docs/manual/reference/method/db.adminCommand

要查看 mongosh 中数据库方法的附加使用详情,请输入 db.<方法名>,不使用括号()。以下示例返回关于 db.adminCommand() 方法的详情

db.adminCommand

输出结果类似于以下内容

[Function: adminCommand] AsyncFunction {
apiVersions: [ 1, Infinity ],
serverVersions: [ '3.4.0', '999.999.999' ],
returnsPromise: true,
topologies: [ 'ReplSet', 'Sharded', 'LoadBalanced', 'Standalone' ],
returnType: { type: 'unknown', attributes: {} },
deprecated: false,
platforms: [ 0, 1, 2 ],
isDirectShellCommand: false,
acceptsRawInput: false,
shellCommandCompleter: undefined,
help: [Function (anonymous)] Help
}

您可以在 mongosh 控制台中查看集合级别的信息。

这些帮助方法接受一个集合名称,<collection>,但您也可以使用通用术语“集合”,甚至可以使用不存在的集合。

要查看当前数据库中集合的列表,使用 show collections 命令

show collections

show collections 的输出指示一个集合是否是 时间序列集合 或只读的 视图。

managementFeedback [view]
survey
weather [time-series]
system.buckets.weather
system.views

在前面示例中

  • managementFeedback 是一个 视图

  • weather 是一个时间序列

  • survey 是一个集合

  • system.buckets.weather 是一个支持 weather 时间序列的系统生成集合

  • system.views 是一个支持在其他集合上创建视图的系统生成集合

要查看集合对象上可用的方法列表,使用 db.<collection>.help() 方法

db.collection.help()

<collection> 可以是现有或不存在集合的名称。

输出类似于以下缩略列表

Collection Class:
aggregate Calculates aggregate values for the data in a collection or a view.
bulkWrite Performs multiple write operations with controls for order of execution.
count Returns the count of documents that would match a find() query for the collection or view.
countDocuments Returns the count of documents that match the query for a collection or view.
deleteMany Removes all documents that match the filter from a collection.
deleteOne Removes a single document from a collection.
...

要在 mongosh 中查看特定收集方法的帮助,请使用 db.<collection>.<method name>,后跟 .help.help()

以下示例显示了 db.collection.insertOne() 方法的帮助:

db.collection.insertOne.help()

输出结果类似于以下内容

db.collection.insertOne(document, options):
Inserts a document into a collection.
For more information on usage: https://mongodb.ac.cn/docs/manual/reference/method/db.collection.insertOne

要查看收集方法的附加详细信息,请输入方法名,db.<collection>.<method>,不包含括号(())。

以下示例返回 insertOne() 方法的详细信息

db.collection.insertOne

输出结果类似于以下内容

[Function: insertOne] AsyncFunction {
apiVersions: [ 1, Infinity ],
serverVersions: [ '3.2.0', '999.999.999' ],
returnsPromise: true,
topologies: [ 'ReplSet', 'Sharded', 'LoadBalanced', 'Standalone' ],
returnType: { type: 'unknown', attributes: {} },
deprecated: false,
platforms: [ 0, 1, 2 ],
isDirectShellCommand: false,
acceptsRawInput: false,
shellCommandCompleter: undefined,
help: [Function (anonymous)] Help
}

要修改使用 读取操作 且使用 find() 的,请使用 光标方法。

要列出可用的修饰符和光标处理方法,请使用 db.collection.find().help() 命令

db.collection.find().help()

此帮助调用接受一个集合名称,<collection>,但您也可以使用通用术语“集合”,甚至可以使用一个不存在的集合。

处理游标的一些有用方法有

  • hasNext()检查游标是否有更多文档。

  • next()返回下一个文档,并将游标位置向前移动一个。

  • forEach(<function>)<function>应用于游标返回的每个文档。

有关可用游标方法的列表,请参阅游标。

mongoshBSON 类提供了帮助方法。帮助方法提供了对 BSON 类的简要概述以及更多信息链接。

要访问 BSON 类的帮助,请在类名或类的实例上运行 .help()

<BSON class>.help()
// or
<BSON class>().help()

例如,要查看 ObjectId BSON 类的帮助,请运行以下命令之一

ObjectId.help()
ObjectId().help()

mongosh 为两种 .help() 方法返回相同的输出

The ObjectId BSON Class:
For more information on usage: https://mongodb.github.io/node-mongodb-native/3.6/api/ObjectID.html

mongosh 为以下 BSON 类提供帮助方法

  • BinData

  • Code

  • DBRef

  • MinKey

  • MaxKey

  • NumberDecimal

  • NumberInt

  • NumberLong

  • ObjectId

  • Symbol (已弃用)

  • Timestamp

mongosh 提供以下方法和命令来包装某些数据库命令并获取有关您的部署的信息

帮助方法和命令
描述
db.help()
显示数据库方法的帮助。
db.<集合>.help()
显示集合方法的帮助。<集合>可以是现有集合的名称或不存在集合的名称。
帮助
显示帮助。
显示集合
显示当前数据库中所有集合的列表。
显示数据库

显示服务器上所有数据库的列表。

show dbsshow databases 同义。

show log <名称>

显示指定记录器名称在内存中的最后一个日志段。如果不指定 <名称>,则命令默认为 global

要显示 startupWarning 日志,运行

show log startupWarnings
show logs
显示可访问的记录器名称。参见 检索 Shell 日志。
show profile
显示最近五次操作,这些操作耗时超过1毫秒。有关更多信息,请参阅数据库分析器的文档。
show roles
显示当前数据库中所有角色列表,包括用户定义的和内置的角色。
show tables
显示当前数据库中所有集合的列表。参见 show collections
show users
显示当前数据库的用户列表。

返回

发行说明