Run a Command运行命令¶
If you specify a callback method, 如果指定回调方法,command()
returns nothing. command()
将不返回任何内容。If you do not specify one, this method returns a 如果未指定,此方法将返回一个Promise
that resolves to the result object when it completes. Promise
,该Promise
在完成时解析为结果对象。See our guide on Promises and Callbacks for more information, or the API documentation for information on the result object.有关更多信息,请参阅我们的承诺和回调指南,或参阅API文档以了解有关结果对象的信息。
You can run all raw database operations using the db.command() method. 可以使用db.command()方法运行所有原始数据库操作。Call the 使用数据库实例上的command对象调用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.command()
方法,以执行诊断和管理任务,如获取服务器统计数据或初始化副本集。
Use the mongo shell for administrative tasks instead of the Node.js driver whenever possible.尽可能使用mongo shell来执行管理任务,而不是Node.js驱动程序。
You can specify additional options in the 可以在options
object passed in the second parameter of the command()
method. command()
方法的第二个参数中传递的options
对象中指定其他选项。For more information on the options you can pass, see the db.command() API documentation. 有关可以传递的选项的更多信息,请参阅db.command()
API文档。You can also pass a callback method as an optional third parameter.还可以将回调方法作为可选的第三个参数传递。
Example示例¶
You can use this example to connect to an instance of MongoDB and interact with a database that contains sample data. 您可以使用此示例连接到MongoDB实例,并与包含示例数据的数据库交互。To learn more about connecting to your MongoDB instance and loading a sample dataset, see the Usage Examples guide.要了解有关连接到MongoDB实例并加载示例数据集的更多信息,请参阅用法示例指南。
import { MongoClient } from "mongodb";
// Replace the uri string with your MongoDB deployment's connection string.用MongoDB部署的连接字符串替换uri字符串。
const uri = "<connection string uri>";
const client = new MongoClient(uri);
async function run() {
try {
await client.connect();
const db = client.db("sample_mflix");
// find the storage statistics for the "sample_mflix" database using the 'dbStats' command
const result = await db.command({
dbStats: 1,
});
console.log(result);
} finally {
await client.close();
}
}
run().catch(console.dir);
import { MongoClient } from "mongodb";
// Replace the uri string with your MongoDB deployment's connection string.用MongoDB部署的连接字符串替换uri字符串。
const uri = "<connection string uri>";
const client = new MongoClient(uri);
async function run() {
try {
await client.connect();
const db = client.db("sample_mflix");
// find the storage statistics for the "sample_mflix" database using the 'dbStats' command
const result = await db.command({
dbStats: 1,
});
console.log(result);
} finally {
await client.close();
}
}
run().catch(console.dir);
The JavaScript and TypeScript code snippets above are identical. 上面的JavaScript和TypeScript代码片段是相同的。There are no TypeScript specific features of the driver relevant to this use case.驱动程序没有与此用例相关的特定于TypeScript的特性。
When you run the preceding command, you should see the following output:运行上述命令时,应看到以下输出:
{
db: 'sample_mflix',
collections: 6,
views: 0,
objects: 75620,
...
}