Run a Command
This version of the documentation is archived and no longer supported. View the current documentation to learn how to upgrade your version of the MongoDB Go Driver.
You can run commands directly on your MongoDB server by using the
RunCommand()
method.
Example
Tip
Read the Usage Examples to learn how to run this example.
The following example retrieves statistics about the
sample_restaurants
database:
db := client.Database("sample_restaurants") command := bson.D{{"dbStats", 1}} var result bson.M err := db.RunCommand(context.TODO(), command).Decode(&result) if err != nil { panic(err) }
View a fully runnable example
Expected Result
After you run the full example, it returns a SingleResult
type
that contains the following values:
// results truncated { "avgObjSize": 548.4101901854896, "collections": 2, "dataSize": 14014074, "db": "sample_restaurants", "indexSize": 286720, ..., }
Note
The result
variable may vary depending on the contents of your
collection.