Docs HomeMongoDB Shell

mongosh Help

This document provides an overview of the help available in mongosh.本文档概述了mongosh中提供的帮助。

Tip

When accessing help in mongosh, you can use the .help() and .help syntaxes interchangeably.mongosh中访问帮助时,可以交替使用help().help语法。

Command Line Help命令行帮助

To see the options for running the mongosh executable and connecting to a deployment, use the --help option from the command line:要查看运行mongosh可执行文件和连接到部署的选项,请在命令行中使用--help选项:

mongosh --help

mongosh Shell Help

To see the list of commands available in the mongosh console, type help inside a running mongosh console:要查看mongosh控制台中可用的命令列表,请在正在运行的mongosh主机中键入help:

help

Database Help数据库帮助

You can view database level information from inside the mongosh console:您可以从mongosh控制台内部查看数据库级别的信息:

By default mongosh shows the current database in the prompt. 默认情况下,mongosh会在提示中显示当前数据库。You can also see the current database by running the db command:您还可以通过运行db命令查看当前数据库:

db

Show Available Databases显示可用数据库

To see the list of databases available to you on the server, use the show dbs command:要查看服务器上可用的数据库列表,请使用show dbs命令:

show dbs

show databases is an alias for show dbs.show databasesshow dbs的别名。

Tip

The list of databases will change depending on your access authorizations. 数据库列表将根据您的访问权限而更改。For more information on access restrictions for viewing databases, see listDatabases.有关查看数据库的访问限制的详细信息,请参阅listDatabases

Show Database Methods显示数据库方法

To see the list of database methods you can use on the db object, run db.help():要查看可以在db对象上使用的数据库方法列表,请运行db.help()

db.help()

The output resembles the following abbreviated list:输出类似于以下缩写列表:

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.
...

Show Help for a Specific Database Method显示特定数据库方法的帮助

To see help for a specific database method in mongosh, type the db.<method name>, followed by .help or .help(). 要查看mongosh中特定数据库方法的帮助,请键入db.<method name>,后跟.help.help()The following example returns help for the db.adminCommand() method:以下示例返回db.adminCommand()方法的帮助:

db.adminCommand.help()

The output resembles the following:输出类似于以下内容:

  db.adminCommand({ serverStatus: 1 }):

Runs an arbitrary command against the admin database.

For more information on usage: https://www.mongodb.com/docs/manual/reference/method/db.adminCommand

Show Additional Usage Details for a Database Method显示数据库方法的其他用法详细信息

To see additional usage details for a database method in mongosh, type the db.<method name> without the parenthesis (()). 要查看mongosh中数据库方法的其他用法详细信息,请键入db.<method name>,不带括号(())。The following example returns details about the db.adminCommand() method:以下示例返回有关db.adminCommand()方法的详细信息:

db.adminCommand

The output resembles the following:输出类似于以下内容:

[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
}

Collection Help集合帮助

You can view collection level information from inside the mongosh console.您可以从mongosh控制台内部查看集合级别的信息。

These help methods accept a collection name, <collection>, but you can also use the generic term, "collection", or even a collection that does not exist.这些帮助方法接受集合名称<collection>,但您也可以使用通用术语“集合”,甚至可以使用不存在的集合。

List Collections in the Current Database列出当前数据库中的集合

To see the list of collections in the current database, use the show collections command:要查看当前数据库中的集合列表,请使用show collections命令:

show collections

The show collections output indicates if a collection is a time series collection or a read-only view.show collections输出指示集合是时间序列集合还是只读视图

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

In the preceding example:在前面的示例中:

  • managementFeedback is a view视图
  • weather is a time series是时间序列
  • survey is a collection是一个集合
  • system.buckets.weather is a system generated collection that supports the weather time series是系统生成的集合,支持weather时间序列
  • system.views is a system generated collection that supports views on other collections是系统生成的集合,支持其他集合上的视图

Show Collection Methods显示集合方法

To see the list of methods available on collection objects use the db.<collection>.help() method:若要查看集合对象上可用方法的列表,请使用数据库db.<collection>.help()方法:

db.collection.help()

<collection> can be the name of an existing or non-existent collection.可以是现有集合或不存在集合的名称。

The output resembles the following abbreviated list:输出类似于以下缩写列表:

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.
...

Show Help for a Specific Collection Method显示特定集合方法的帮助

To see help for a specific collection method in mongosh, use db.<collection>.<method name>, followed by .help or .help().要查看mongosh中特定集合方法的帮助,请使用db.<collection>.<method name>,后跟.help.help()

The following example displays help for db.collection.insertOne():以下示例显示db.collection.insertOne()的帮助:

db.collection.insertOne.help()

The output resembles the following:输出类似于以下内容:

db.collection.insertOne(document, options):

Inserts a document into a collection.将文档插入集合中。

For more information on usage: https://www.mongodb.com/docs/manual/reference/method/db.collection.insertOne

Show Additional Details for a Collection Method显示集合方法的其他详细信息

To see additional details for a collection method type the method name, db.<collection>.<method>, without the parenthesis (()).若要查看集合方法的其他详细信息,请键入db.<collection>.<method>,不带括号(())。

The following example returns details about the insertOne() method:以下示例返回有关insertOne()方法的详细信息:

db.collection.insertOne

The output resembles the following:输出类似于以下内容:

[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
}

Cursor Help游标帮助

To modify read operations that use find(), use cursor methods.要修改使用find()的读取操作,请使用游标方法。

To list the available modifier and cursor handling methods, use the db.collection.find().help() command:要列出可用的修饰符和游标处理方法,请使用db.collection.find().help()命令:

db.collection.find().help()

This help call accepts a collection name, <collection>, but you can also use the generic term, "collection", or even a collection which does not exist.此帮助调用接受集合名称<collection>,但您也可以使用通用术语“集合”,甚至可以使用不存在的集合。

Some useful methods for handling cursors are:处理游标的一些有用方法有:

  • hasNext() checks if the cursor has more documents.检查游标是否有更多文档。
  • next() returns the next document and moves the cursor position forward by one.返回下一个文档,并将游标位置向前移动一个。
  • forEach(<function>) applies the <function> to each document returned by the cursor.<function>应用于游标返回的每个文档。

For a list of available cursor methods, see Cursor.有关可用游标方法的列表,请参阅游标

BSON Class Help

mongosh provides help methods for BSON classes. 提供了BSON类的帮助方法。The help methods provide a brief overview of the BSON class and a link with more information.help方法提供了BSON类的简要概述以及包含更多信息的链接。

To access help for BSON classes, run .help() on either the class name or an instantiated instance of the class:要访问BSON类的帮助,请对类名或类的实例化实例运行.help()

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

For example, to view help for the ObjectId BSON class, run one of the following commands:例如,要查看ObjectId BSON类的帮助,请运行以下命令之一:

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

mongosh returns the same output for both .help() methods:为两个.help()方法返回相同的输出:

The ObjectId BSON Class:

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

mongosh provides help methods for the following BSON classes:为以下BSON类提供了帮助方法:

  • BinData
  • Code
  • DBRef
  • MinKey
  • MaxKey
  • NumberDecimal
  • NumberInt
  • NumberLong
  • ObjectId
  • Symbol (Deprecated)
  • Timestamp

Command Helpers命令帮助程序

mongosh provides the following methods and commands to wrap certain database commands and obtain information on your deployment:提供了以下方法和命令来包装某些数据库命令并获取有关部署的信息:

Help Methods and Commands帮助方法和命令Description描述
db.help()Display help for database methods.显示数据库方法的帮助。
db.<collection>.help()Display help on collection methods. 显示有关集合方法的帮助。The <collection> can be the name of an existing collection or a non-existing collection.<collection>可以是现有集合的名称,也可以是不存在的集合的名称。
helpDisplay help.显示帮助。
show collectionsDisplay a list of all collections for current database.显示当前数据库的所有集合的列表。
show dbsDisplay a list of all databases on the server.显示服务器上所有数据库的列表。
Note

show dbs is synonymous with show databases.show databases的同义词。

show log <name>Displays the last segment of log in memory for the specified logger name. 显示指定记录器名称在内存中的最后一段日志。If you do not specify a <name>, the command defaults to global.如果未指定<name>,则该命令默认为global
To show startupWarning logs, run:要显示startupWarning日志,请运行:
show log startupWarnings
show logsDisplay the accessible logger names. 显示可访问的记录器名称。See Retrieve Shell Logs.请参见检索Shell日志
show profileDisplay the five most recent operations that took 1 millisecond or more. 显示花费1毫秒或更长时间的最近五次操作。See documentation on the database profiler for more information.有关详细信息,请参阅数据库探查器的文档。
show rolesDisplay a list of all roles, both user-defined and built-in, for the current database.显示当前数据库的所有角色的列表,包括用户定义的角色和内置的角色。
show tablesDisplay a list of collections in the current database. 显示当前数据库中集合的列表。See show collections.请参见show collections
show usersDisplay a list of users for current database.显示当前数据库的用户列表。