Docs Menu

Docs HomeDevelop ApplicationsMongoDB DriversNode.js

Run a Command

Note

If you specify a callback method, command() returns nothing. If you do not specify one, this method returns a Promise that resolves to the result object when it completes. See our guide on Promises and Callbacks for more information, or the API documentation for information on the result object.

You can run all raw database operations using the db.command() method. Call the command() method with your command object on an instance of a database for diagnostic and administrative tasks such as fetching server stats or initializing a replica set.

Note

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

You can specify additional options in the options object passed in the second parameter of the command() method. For more information on the options you can pass, see the db.command() API documentation. You can also pass a callback method as an optional third parameter.

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.

When you run the preceding command, you should see the following output:

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