Run a Command
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.
Example
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, ... }