对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。

快速参考

此页面显示Go驱动程序中多个MongoDB操作的驱动程序语法,并提供相关参考和API文档的链接。

操作
语法
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{ {"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)
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)

更新文档中的数组

API 文档
Update Arrays Guide

result, err := coll.UpdateMany( context.TODO(), bson.D{}, bson.D{{"$push", bson.D{{"hobbies", "painting"}}}})
result, err := coll.ReplaceOne( context.TODO(), bson.D{{"firstName", "Mick"}, {"lastName", "Salazar"}}, bson.D{{"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)
pipeline := mongo.Pipeline{bson.D{{"$match", bson.D{{"operationType", "insert"}}}}}cs, err := coll.Watch(context.TODO(), pipeline)

从游标迭代访问数据

API 文档
分别检索文档参考

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

以数组形式访问游标中的数据

API 文档
检索所有文档参考

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

列出不同字段值

API 文档
Distinct Values Guide

var results []stringerr = coll.Distinct(context.TODO(), "firstName", bson.D{}).Decode(&results)

限制检索的文档数量

API 文档
限制文档参考

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

跳过检索到的文档

API 文档
跳过文档引用

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

结果中的项目文档字段

API 文档
投影指南

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 on fields covered by text indexescursor, err := coll.Find(context.TODO(), bson.D{{"$text", bson.D{{"$search", "arts"}}}})
给本页内容打分