0
点赞
收藏
分享

微信扫一扫

mongo 创建索引


三种方式:

方式一:使用可视化工具Robo 3T 创建

右键 选择add index  添加索引

如下图:

mongo 创建索引_右键

mongo 创建索引_可视化工具_02

 

方式二:

脚本创建:

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

举报

相关推荐

0 条评论