mongosh Help (original) (raw)

This document provides an overview of the help available inmongosh.

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 --helpoption from the command line:

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

You can view database level information from inside themongosh console:

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

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

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, rundb.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 thedb.<method name>, followed by .help or .help(). The following example returns help for the db.adminCommand()method:

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 thedb.adminCommand() method:

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 themongosh 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 theshow collections command:

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


managementFeedback          [view]

survey

weather                     [time-series]

system.buckets.weather

system.views

In the preceding example:

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

<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, usedb.<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 theinsertOne() method:

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 usefind(), use cursor methods.

To list the available modifier and cursor handling methods, use thedb.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:

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:

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:

mongosh provides help for log messagemethods. To see available log message methods, run the following command:

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..help() Display help on collection methods. The can be the name of an existing collection or a non-existing collection.
help Display help.
history() Display a list of previously executed commands. Available starting in MongoDB Shell 2.4.0.
show collections Display a list of all collections for current database.
show dbs Display a list of all databases on the server.show dbs is synonymous with show databases.
show log Displays the last segment of log in memory for the specified logger name. If you do not specify a , the command defaults to global.To show startupWarning logs, run:show log startupWarnings
show logs Display the accessible logger names. See Shell Logs.
show profile Display the five most recent operations that took 1 millisecond or more. See documentation on thedatabase 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.