Docs 菜单

Docs 主页查看和分析数据MongoDB Shell

mongosh 帮助

在此页面上

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

本文档概述了 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.<method name> ,然后键入.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://www.mongodb.com/zh-cn/docs/manual/reference/method/db.adminCommand

要查看mongosh db.<method name>中数据库方法的其他使用详细信息,请键入不带括号 (() ) 的 。以下示例返回有关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://www.mongodb.com/zh-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.<collection>.help()
显示有关集合方法的帮助。<collection>可以是现有集合或不存在集合的名称。
help
显示帮助。
show collections
显示当前数据库中所有集合的列表。
show dbs

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

注意

show dbsshow databases 同义。

show log <name>

显示指定记录器名称的内存日志的最后一段。 如果没有指定<name>,命令默认为global

要显示 startupWarning 日志,请运行:

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