如何使用golang mongo-driver在mongoDB中创建复合索引?

huangapple go评论123阅读模式
英文:

How to create compound indexes in mongoDB using golang mongo-driver?

问题

我正在尝试在数据库中创建一个索引,但是CreateMany方法会在指定的列数组上为每个列创建索引。

但是我需要为一对列创建一个复合索引。

英文:

I am trying to create an index in the database, but, CreateMany creates indexes on individual columns on the array of columns specified.

But I require a compound index for a couple of columns

答案1

得分: 2

您可以使用值为mongo.IndexModel的索引来指定索引:

  1. type IndexModel struct {
  2. // 描述应该用于索引的键的文档。它不能为nil。这必须是一个保持顺序的类型,如bson.D。不支持映射类型,如bson.M。请参阅https://docs.mongodb.com/manual/indexes/#indexes
  3. // 了解有效文档的示例。
  4. Keys interface{}
  5. // 用于创建索引的选项。
  6. Options *options.IndexOptions
  7. }

可索引的属性(或属性)在Keys字段中给出。它不一定必须包含一个单独的属性。根据其名称(Keys)和文档提示:它可以包含多个属性,并且由于顺序很重要,应该使用保持顺序的数据结构列出,例如bson.D

下面是一个示例,创建了两个复合索引,第一个索引使用prop1prop2prop3,第二个索引使用prop4prop5

  1. ctx := context.Background()
  2. c := db.Collection("collname")
  3. _, err = c.Indexes().CreateMany(ctx, []mongo.IndexModel{
  4. // prop1, prop2, prop3
  5. {
  6. Keys: bson.D{
  7. {Key: "prop1", Value: 1}, // prop1升序
  8. {Key: "prop2", Value: 1}, // prop2升序
  9. {Key: "prop3", Value: -1}, // prop3降序
  10. },
  11. },
  12. // prop4, prop5
  13. {
  14. Keys: bson.D{
  15. {Key: "prop4", Value: 1}, // prop4升序
  16. {Key: "prop5", Value: 1}, // prop5升序
  17. },
  18. },
  19. })
  20. if err != nil {
  21. log.Printf("Failed to create indices: %v", err)
  22. }
英文:

You specify an index with a value of mongo.IndexModel:

  1. type IndexModel struct {
  2. // A document describing which keys should be used for the index. It cannot be nil. This must be an order-preserving
  3. // type such as bson.D. Map types such as bson.M are not valid. See https://docs.mongodb.com/manual/indexes/#indexes
  4. // for examples of valid documents.
  5. Keys interface{}
  6. // The options to use to create the index.
  7. Options *options.IndexOptions
  8. }

The indexable property (or properties) are given in the Keys field. It does not necessary have to contain a single property. As its name (Keys) and doc hits: it may contain multiple properties, and since order matters, it should be listed with a data structure that preservers order, such as bson.D.

Here's an example creating 2 compound indices, first one using prop1, prop2 and prop3, the second one using prop4 and prop4.

  1. ctx := context.Background()
  2. c := db.Collection("collname")
  3. _, err = c.Indexes().CreateMany(ctx, []mongo.IndexModel{
  4. // prop1, prop2, prop3
  5. {
  6. Keys: bson.D{
  7. {Key: "prop1", Value: 1}, // prop1 asc
  8. {Key: "prop2", Value: 1}, // prop2 asc
  9. {Key: "prop3", Value: -1}, // prop3 desc
  10. },
  11. },
  12. // prop4, prop5
  13. {
  14. Keys: bson.D{
  15. {Key: "prop4", Value: 1}, // prop4 asc
  16. {Key: "prop5", Value: 1}, // prop5 asc
  17. },
  18. },
  19. })
  20. if err != nil {
  21. log.Printf("Failed to create indices: %v", err)
  22. }

huangapple
  • 本文由 发表于 2021年10月7日 04:45:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/69472471.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定