Docs Menu

Quick Reference

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

Command
Syntax
Find a Document

API Documentation
err = coll.FindOne(context.TODO(), bson.D{{"rating", 5}}).Decode(&result)
cursor, err := coll.Find(context.TODO(), bson.D{{"rating", bson.D{{"$gte", 8}}}})
result, err := coll.InsertOne(
context.TODO(),
bson.D{
{"type", "Masala"},
{"rating", 10},
{"vendor", bson.A{"A", "C"}}
}
)
docs := []interface{} {
bson.D{{"type", "English Breakfast"}, {"rating", 6}},
bson.D{{"type", "Oolong"}, {"rating", 7}, {"vendor", bson.A{"C"}}},
bson.D{{"type", "Assam"}, {"rating", 5}},
bson.D{{"type", "Earl Grey"}, {"rating", 8}, {"vendor", bson.A{"A", "B"}}},
}
result, err := coll.InsertMany(context.TODO(), docs)
result, err := coll.UpdateOne(
context.TODO(),
bson.D{{"type", "Oolong"}},
bson.D{{"$set", bson.D{{"rating", 8}}}}
)
result, err := coll.UpdateMany(
context.TODO(),
bson.D{{"rating", bson.D{{"$lt", 10}}}},
bson.D{{"$inc", bson.D{{"rating", 2}}}}
)
Update Arrays in Documents

result, err := coll.UpdateMany(
context.TODO(),
bson.D{},
bson.D{{"$push", bson.D{{"vendor", "D"}}}}
)
result, err := coll.ReplaceOne(
context.TODO(),
bson.D{{"type", "Oolong"}},
bson.D{{"type", "Jasmine"}, {"rating", 9}}
)
result, err := coll.DeleteOne(
context.TODO(),
bson.D{{"type", "Earl Grey"}}
)
results, err := coll.DeleteMany(
context.TODO(),
bson.D{{"rating", bson.D{{"$gt", 7}}}}
)
models := []mongo.WriteModel{
mongo.NewInsertOneModel().SetDocument(bson.D{{"type", "Chrysanthemum"}, {"rating", 5}}),
mongo.NewUpdateOneModel().SetFilter(bson.D{{"type", "Jasmine"}}).
SetUpdate(bson.D{{"$set", bson.D{{"type", "Oolong"}}}}),
}
opts := options.BulkWrite().SetOrdered(true)
results, err := coll.BulkWrite(context.TODO(), models, opts)
pipeline := mongo.Pipeline{bson.D{{"$match", bson.D{{"operationType", "insert"}}}}}
cs, err := coll.Watch(context.TODO(), pipeline)
Access Data from a Cursor Iteratively

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

cursor, err := coll.Find(context.TODO(), bson.D{})
var results []bson.D
if 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
results, err := coll.Distinct(context.TODO(), "type", bson.D{})
Limit the Number of Documents Retrieved

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

// the collection has 6 documents
cursor, err := coll.Find(context.TODO(), bson.D{}, options.Find().SetSkip(4))
Sort the Documents When Retrieving Them

cursor, err := coll.Find(context.TODO(), bson.D{}, options.Find().SetSort(bson.D{{"rating", 1}}))
Project Document Fields When Retrieving Them

cursor, err := coll.Find(
context.TODO(),
bson.D{},
options.Find().SetProjection(
bson.D{{"vendor", 0}, {"_id",0}}
)
)
Create an Index

model := mongo.IndexModel{Keys: bson.D{{"type", 1}, {"rating", -1}}}
name, err := coll.Indexes().CreateOne(context.TODO(), model)
// only searches fields with text indexes
cursor, err := coll.Find(context.TODO(), bson.D{{"$text", bson.D{{"$search", "Breakfast"}}}})
←  Quick StartUsage Examples →
Give Feedback
© 2022 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.