对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

运行命令示例

您可以使用 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()