Docs Menu

Docs HomeDevelop ApplicationsMongoDB DriversNode.js Driver

Run a Command

You can execute database commands by using the command() method on a Db instance.

You can specify a command and options in a document. To run the command, pass this document to the command() method. To see a full list of database commands, see Database Commands in the Server manual.

Tip

Use the MongoDB Shell for administrative tasks instead of the Node.js driver whenever possible.

You can specify optional command behavior by passing a RunCommandOptions object to the command() method. To learn more about the supported options, see the Db.command() API documentation.

Note

You can use this example to connect to an instance of MongoDB and interact with a database that contains sample data. To learn more about connecting to your MongoDB instance and loading a sample dataset, see the Usage Examples guide.

Note

Identical Code Snippets

The JavaScript and TypeScript code snippets above are identical. There are no TypeScript specific features of the driver relevant to this use case.

Running the preceding command, you see the following output:

{
db: 'sample_mflix',
collections: 6,
views: 0,
objects: 75620,
...
}
←  Retrieve Distinct Values of a FieldWatch for Changes →