Navigation

Access Help in mongosh

On this page

  • Command Line Help
  • Shell Help
  • Database Help
  • Collection Help
  • BSON Class Help
  • Command Helpers

mongosh provides additional help information in its help system. This document provides an overview of accessing this help information.

Tip

When accessing help in mongosh, you can use the .help() and .help syntaxes interchangeably.

To see the list of options and help for starting mongosh, use the --help option from the command line:

mongosh --help

To see a basic list of commands you can run, in mongosh, type help:

help
Show List of Databases

To see the list of databases on the server, use the show dbs command:

show dbs
Note

show databases is an alias for show dbs.

Show List of Database Methods

To see the list of methods you can use on the db object, call the db.help() method or run the db.help command:

db.help()

The output resembles the following:

Database Class:
getMongo Returns the current database connection
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.
aggregate Runs a specified admin/diagnostic
pipeline which does not require an
underlying collection.
getSiblingDB Returns another database without
modifying the db variable in the shell
environment.
getCollection Removes the current database, deleting
the associated data files.
dropDatabase Removes the current database, deleting
the associated data files.
Show Help for Specific Database Method

To see help for a specific database method in mongosh, type the db.<method name>, followed by .help or .help(). The following example returns help for the db.adminCommand() method:

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://docs.mongodb.com/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 (()). The following example returns details about the db.adminCommand() method:

db.adminCommand

The output resembles the following:

[Function] {
serverVersions: [ '3.4.0', '4.4.0' ],
returnsPromise: true,
topologies: [ 0, 2, 1 ],
returnType: { type: 'unknown', attributes: {} },
platforms: [ 0, 1, 2 ],
help: [Function] Help
}
Show List of Collections

To see the list of collections in the current database, use the show collections command:

show collections
Show List of Collection Methods

To see the list of methods available on the collection objects (e.g. db.<collection>), use the db.<collection>.help() method:

db.collection.help()

<collection> can be the name of an existing or nonexistent collection.

The output resembles the following:

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.
distinct Finds the distinct values for a specified
field across a single collection or view
and returns the results in an array.
estimatedDocumentCount Returns the count of all documents in a
collection or view.
find Selects documents in a collection or view.
...
Show Help for Specific Collection Method

To see help for a specific collection method in mongosh, type the db.<collection>.<method name>, followed by .help or .help(). The following example returns help for the db.collection.insertOne() method:

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://docs.mongodb.com/manual/reference/method/db.collection.insertOne
Show Additional Usage Details for a Collection Method

To see additional usage details for a collection method in mongosh, type the db.<collection>.<method> name without the parenthesis (()). The following example returns details about the insertOne() method:

db.collection.insertOne

The output resembles the following:

[Function] {
serverVersions: [ '3.2.0', '4.4.0' ],
returnsPromise: true,
topologies: [ 0, 2, 1 ],
returnType: { type: 'unknown', attributes: {} },
platforms: [ 0, 1, 2 ],
help: [Function] Help
}

mongosh provides help methods for BSON classes. The help methods provide a brief overview of the BSON class and a link with more information.

To access help for BSON classes, run .help() on either the class name or an instantiated instance of the class:

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

For example, to view help for the ObjectId BSON class, run one of the following commands:

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

mongosh returns the same output for both .help() methods:

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:

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

mongosh provides the following methods and commands to wrap certain database commands and obtain information on your deployment:

Help Methods and Commands
Description
help
Show help.
db.help()
Show help for database methods.
db.<collection>.help()
Show help on collection methods. The <collection> can be the name of an existing collection or a non-existing collection.
show dbs

Print a list of all databases on the server.

Note

show dbs is synonymous with show databases.

use <db>
Switch current database to <db>. The mongosh variable db is set to the current database.
show collections
Print a list of all collections for current database
show users
Print a list of users for current database.
show roles
Print a list of all roles, both user-defined and built-in, for the current database.
show profile
Print the five most recent operations that took 1 millisecond or more. See documentation on the database profiler for more information.
show logs
Print the accessible logger names. See Retrieve Shell Logs.
show log <name>

Prints the last segment of log in memory for the specified logger name. If you do not specify a <name>, the command defaults to global.

Example

To show startupWarning logs, run:

show log startupWarnings
load()
Execute a JavaScript file. See Write Scripts for mongosh for more information.
Give Feedback

On this page

  • Command Line Help
  • Shell Help
  • Database Help
  • Collection Help
  • BSON Class Help
  • Command Helpers