文档菜单
文档首页
/ / /
Go 驱动
/

运行命令

您可以通过使用RunCommand() 方法直接在您的 MongoDB 服务器上运行命令。

提示

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

以下示例检索关于 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()

返回

不同字段值