For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

Quick Reference

This page shows the driver syntax for several MongoDB operations in the Go driver and provides links to related reference and API documentation.

Operation
Syntax
err = coll.FindOne(context.TODO(), bson.D{{"firstName", Mike}}).Decode(&result)
[{firstName Mike}, {lastName Smith} ...]
cursor, err := coll.Find(context.TODO(), bson.D{{"age", bson.D{{"$gte", 46}}}})
[{firstName Kyle}, {age 51}, ... ]
[{firstName Omar}, {age 47}, ... ]
result, err := coll.InsertOne(
context.TODO(),
bson.D{
{"firstName", "Aliya"},
{"lastName", "Sanders"}
}
)
docs := []any {
bson.D{{"firstName", "Erik"}, {"age", 27}},
bson.D{{"firstName", "Mohammad"}, {"lastName", "Ahmad"}, {"age", 10}},
bson.D{{"firstName", "Todd"}},
bson.D{{"firstName", "Juan"}, {"lastName", "Pablo"}}
}
result, err := coll.InsertMany(context.TODO(), docs)
result, err := coll.UpdateOne(
context.TODO(),
bson.D{{"firstName", "Erik"}},
bson.D{{"$set", bson.D{{"age", 28}}}}
)
fmt.Printf("Number of modified documents: %d\n", result.ModifiedCount)
Number of modified documents: 1
result, err := coll.UpdateMany(
context.TODO(),
bson.D{{"age", bson.D{{"$gte", 65}}}},
bson.D{{"$set", bson.D{{"classification", "senior"}}}}
)
fmt.Printf("Number of modified documents: %d\n", result.ModifiedCount)
Number of modified documents: 4

Update Arrays in Documents

API Documentation
Update Arrays Guide

result, err := coll.UpdateMany(
context.TODO(),
bson.D{},
bson.D{{"$push", bson.D{{"hobbies", "painting"}}}}
)
[{firstName Xiao}, {hobbies ["painting"]}, ... ]
[{firstName Omar}, {hobbies ["kayaking", "painting"]}, ... ]
...
result, err := coll.ReplaceOne(
context.TODO(),
bson.D{{"firstName", "Mick"}, {"lastName", "Salazar"}},
bson.D{{"firstName", "Michael"}, {"lastName", "Salazar Jr."}}
)
[{{firstName Michael}, {lastName Salazar Jr.} }]
result, err := coll.DeleteOne(
context.TODO(),
bson.D{{"firstName", "Xiao"}}
)
results, err := coll.DeleteMany(
context.TODO(),
bson.D{{"age", bson.D{{"$lte", 12}}}}
)
models := []mongo.WriteModel{
mongo.NewInsertOneModel().SetDocument(bson.D{{"firstName", "John"}, {"age", 5}}),
mongo.NewUpdateOneModel().SetFilter(bson.D{{"firstName", "Juan"}}).
SetUpdate(bson.D{{"$set", bson.D{{"age", 12}}}}),
}
opts := options.BulkWrite().SetOrdered(true)
results, err := coll.BulkWrite(context.TODO(), models, opts)
[{firstName John}, {age 5} ... ]
[{firstName Juan}, {age 12} ... ]
pipeline := mongo.Pipeline{bson.D{{"$match", bson.D{{"operationType", "insert"}}}}}
cs, err := coll.Watch(context.TODO(), pipeline)
cursor, err := coll.Find(context.TODO(), bson.D{})
for cursor.Next(context.TODO()) {
var result bson.D
if err := cursor.Decode(&result); err != nil {
log.Fatal(err)
}
fmt.Println(result)
}
[{firstName Doug} ... ]
[{firstName Erik} ...]
[{firstName Oleg} ...]
...

Access Data from a Cursor as an Array

API Documentation
Retrieve All Documents Reference

cursor, err := coll.Find(context.TODO(), bson.D{})
var results []bson.D
if err = cursor.All(context.TODO(), &results); err != nil {
panic(err)
}
[{firstName Mike} ... ]
[{firstName Edgar} ...]
[{firstName Freddie} ...]
...
count, err := coll.CountDocuments(context.TODO(), bson.D{})
17

List Distinct Field Values

API Documentation
Distinct Values Guide

var results []string
err = coll.Distinct(context.TODO(), "firstName", bson.D{}).Decode(&results)
[Mike Xiao Sandy ...]

Limit the Number of Documents Retrieved

API Documentation
Limit Documents Reference

cursor, err := coll.Find(context.TODO(), bson.D{}, options.Find().SetLimit(2))
[{firstName Xiao} ... ]
[{firstName Oleg} ...]
// the collection has 6 documents
cursor, err := coll.Find(context.TODO(), bson.D{}, options.Find().SetSkip(4))
[{firstName Sandy} ... ]
[{firstName Michael} ...]
cursor, err := coll.Find(context.TODO(), bson.D{}, options.Find().SetSort(bson.D{{"age", 1}}))
[{firstName Dev} {age 5} ... ]
[{firstName Jose} {age 7} ... ]
[{firstName Om} {age 8} ... ]

Project Document Fields in Results

API Documentation
Projection Guide

cursor, err := coll.Find(
context.TODO(),
bson.D{},
options.Find().SetProjection(
bson.D{{"age", 0}, {"_id",0}}
)
)
[{firstName Lester} {lastName Franklin}]
[{firstName Wendall} {lastName Griffin}]
...
model := mongo.IndexModel{Keys: bson.D{{"firstName", 1}, {"lastName", -1}}}
name, err := coll.Indexes().CreateOne(context.TODO(), model)
// Only searches on fields covered by text indexes
cursor, err := coll.Find(context.TODO(), bson.D{{"$text", bson.D{{"$search", "arts"}}}})
[{"firstName": "Emily" , "biography": "I am involved with arts and culture in my community."} ... ]
[{"firstName": "Juan" , "biography": "I attended the School of Arts for cello performance."} ... ]