"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/readpref"
database := client.Datbase("haha")
database.CreateCollection(context.TODO(),“hehe”")
mod2 := mongo.IndexModel{
Keys: bson.D {
{"name",1},{"age",1},
}, Options: nil,
}
s, err := coll.Indexes().CreateOne(context.TODO(),mod2)
if err != nil {
fmt.Println("Indexes().CreateOne() ERROR:", err)
} else {
fmt.Println("CreateOne() index:", s) // 索引名字name_1_age_1
fmt.Println("CreateOne() type:", reflect.TypeOf(ind), "\n")// 类型 string
}
本文介绍了如何使用Go语言的MongoDB驱动库进行数据库操作,包括连接数据库、创建集合和创建索引。通过实例展示了创建集合`hehe`并设置复合索引的过程。
5万+

被折叠的 条评论
为什么被折叠?



