Docs Menu

Docs HomeView & Analyze DataMongoDB Shell

mongosh Help

On this page

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

This document provides an overview of the help available in mongosh.

Tip

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

To see the options for running the mongosh executable and connecting to a deployment, use the --help option from the command line:

mongosh --help

To see the list of commands available in the mongosh console, type help inside a running mongosh console:

help

You can view database level information from inside the mongosh console:

By default mongosh shows the current database in the prompt. You can also see the current database by running the db command:

db

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

show dbs

show databases is an alias for show dbs.

Tip

The list of databases will change depending on your access authorizations. For more information on access restrictions for viewing databases, see listDatabases.

To see the list of database methods you can use on the db object, run 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.
...

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://www.mongodb.com/docs/manual/reference/method/db.adminCommand

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

You can view collection level information from inside the mongosh console.

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.

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

show collections

The show collections output indicates if a collection is a time series collection or a read-only view.

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

  • system.views is a system generated collection that supports views on other collections

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

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

To see help for a specific collection method in mongosh, use db.<collection>.<method name>, followed by .help or .help().

The following example displays help for 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

To see additional details for a collection method type the method name, db.<collection>.<method>, without the parenthesis (()).

The following example returns details about the insertOne() method:

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
}

To modify read operations that use find(), use cursor methods.

To list the available modifier and cursor handling methods, use the db.collection.find().help() command:

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.

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.

For a list of available cursor methods, see Cursor.

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
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.
help
Display help.
show collections
Display a list of all collections for current database.
show dbs

Display a list of all databases on the server.

Note

show dbs is synonymous with 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.

To show startupWarning logs, run:

show log startupWarnings
show logs
Display the accessible logger names. See Retrieve Shell Logs.
show profile
Display the five most recent operations that took 1 millisecond or more. See documentation on the database profiler for more information.
show roles
Display a list of all roles, both user-defined and built-in, for the current database.
show tables
Display a list of collections in the current database. See show collections.
show users
Display a list of users for current database.
←  Release Notes