mongo 创建索引
三种方式:
方式一:使用可视化工具Robo 3T 创建
右键 选择add index 添加索引
如下图:
方式二:
脚本创建:
db.test.createindex({"phone":1})
方式三:
使用代码创建:
db.collection.indexs().create({})
//两个字段关联 ,创建唯一索引
_, err = mongoCli.Client.Collection("userinfo").Indexes().CreateOne(context.Background(), mongo.IndexModel{ Keys: bsonx.Doc{{"phone", bsonx.Int32(1)}, {"cid", bsonx.Int32(1)}}, Options: options.Index().SetUnique(true), })