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.

Quick Reference

This page shows the driver syntax for several MongoDB commands and links to their related reference and API documentation.

Command
Syntax
err = coll.FindOne(context.TODO(), bson.D{{"firstName", Mike}}).Decode(&result)
cursor, err := coll.Find(context.TODO(), bson.D{{"age", bson.D{{"$gte", 46}}}})
result, err := coll.InsertOne( context.TODO(), bson.D{ {"animal", "Dog"}, {"breed", "Beagle"} })
docs := []interface{} { 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("The number of modified documents: %d\n", result.ModifiedCount)
result, err := coll.UpdateMany( context.TODO(), bson.D{{"age", bson.D{{"$gte", 58}}}}, bson.D{{"$set", bson.D{{"description", "Senior"}}}})fmt.Printf("The number of modified documents: %d\n", result.ModifiedCount)

Update Arrays in Documents

API Documentation
Fundamentals

result, err := coll.UpdateMany( context.TODO(), bson.D{}, bson.D{{"$push", bson.D{{family, "brother"}}}})
result, err := coll.ReplaceOne( context.TODO(), bson.D{{"firstName", "Mick"}}, bson.D{{"firstName", "Mike"}, {"lastName", "Doe"}})
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)

Monitor Data Changes

API Documentation
Usage Example

pipeline := mongo.Pipeline{bson.D{{"$match", bson.D{{"operationType", "insert"}}}}}cs, err := coll.Watch(context.TODO(), pipeline)

Access Data from a Cursor Iteratively

API Documentation
Fundamentals

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)}

Access Data from a Cursor as an Array

API Documentation
Fundamentals

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

List the Distinct Documents or Field Values
API Documentation
Usage Example
Fundamentals

results, err := coll.Distinct(context.TODO(), "firstName", bson.D{})

Limit the Number of Documents Retrieved

API Documentation
Fundamentals

cursor, err := coll.Find(context.TODO(), bson.D{}, options.Find().SetLimit(2))

Skip Retrieved Documents

API Documentation
Fundamentals

// the collection has 6 documentscursor, err := coll.Find(context.TODO(), bson.D{}, options.Find().SetSkip(4))

Sort the Documents When Retrieving Them

API Documentation
Fundamentals

cursor, err := coll.Find(context.TODO(), bson.D{}, options.Find().SetSort(bson.D{{"age", 1}}))

Project Document Fields When Retrieving Them

API Documentation
Fundamentals

cursor, err := coll.Find( context.TODO(), bson.D{}, options.Find().SetProjection( bson.D{{"age", 0}, {"_id",0}} ))
model := mongo.IndexModel{Keys: bson.D{{"firstName", 1}, {"lastName", -1}}}name, err := coll.Indexes().CreateOne(context.TODO(), model)
// only searches fields with text indexescursor, err := coll.Find(context.TODO(), bson.D{{"$text", bson.D{{"$search", "beagle"}}}})
Rate this page