三种方式:

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

右键 选择add index  添加索引

如下图:mongo 创建索引

mongo 创建索引

 

方式二:

脚本创建:

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

相关文章:

  • 2021-10-12
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-12
  • 2022-12-23
  • 2021-11-27
  • 2021-08-02
猜你喜欢
  • 2022-02-01
  • 2022-12-23
  • 2021-11-27
  • 2021-12-09
  • 2021-07-27
  • 2022-12-23
相关资源
相似解决方案