AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。

コマンド例の実行

RunCommand()メソッドを使用して、MongoDB サーバー上でコマンドを直接実行できます。

Tip

この例を実行する方法については、「使用例」をお読みください。

次の例では、 sample_restaurantsデータベースに関する統計を取得します。

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

実行可能な例を表示

完全な例を実行すると、次の値を含むSingleResult型が返されます。

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

注意

result変数はコレクションの内容によって異なる場合があります。

RunCommand()

このページを評価