“文档” 菜单
文档首页
/ / /
Go 驱动程序
/

运行命令

您可以使用 RunCommand()方法直接在 MongoDB Server 上运行命令。

提示

阅读用法示例,了解如何运行此示例。

以下示例检索有关 sample_restaurants 数据库的统计信息:

db := client.Database("sample_restaurants")
// Retrieves statistics about the specified database
command := bson.D{{"dbStats", 1}}
var result bson.M
// Runs the command and prints the database statistics
err := db.RunCommand(context.TODO(), command).Decode(&result)
// Prints a message if any errors occur during the command execution
if err != nil {
panic(err)
}

查看 完全可运行的示例

运行完整示例后,它将返回包含以下值的 SingleResult 类型:

// results truncated
{
"avgObjSize": 548.4101901854896,
"collections": 2,
"dataSize": 14014074,
"db": "sample_restaurants",
"indexSize": 286720,
...,
}

注意

result 变量可能会根据集合的内容而有所不同。

RunCommand()

← 检索字段的不同值