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

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

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的索引来指定索引:

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

    // 用于创建索引的选项。
    Options *options.IndexOptions
}

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

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

ctx := context.Background()
c := db.Collection("collname")

_, err = c.Indexes().CreateMany(ctx, []mongo.IndexModel{
    // prop1, prop2, prop3
    {
        Keys: bson.D{
            {Key: "prop1", Value: 1},   // prop1升序
            {Key: "prop2", Value: 1},   // prop2升序
            {Key: "prop3", Value: -1},  // prop3降序
        },
    },
    // prop4, prop5
    {
        Keys: bson.D{
            {Key: "prop4", Value: 1},   // prop4升序
            {Key: "prop5", Value: 1},   // prop5升序
        },
    },
})
if err != nil {
    log.Printf("Failed to create indices: %v", err)
}
英文:

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

type IndexModel struct {
	// A document describing which keys should be used for the index. It cannot be nil. This must be an order-preserving
	// type such as bson.D. Map types such as bson.M are not valid. See https://docs.mongodb.com/manual/indexes/#indexes
	// for examples of valid documents.
	Keys interface{}

	// The options to use to create the index.
	Options *options.IndexOptions
}

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.

ctx := context.Background()
c := db.Collection("collname")

_, err = c.Indexes().CreateMany(ctx, []mongo.IndexModel{
	// prop1, prop2, prop3
	{
		Keys: bson.D{
			{Key: "prop1", Value: 1}, // prop1 asc
			{Key: "prop2", Value: 1}, // prop2 asc
			{Key: "prop3", Value: -1}, // prop3 desc
		},
	},
	// prop4, prop5
	{
		Keys: bson.D{
			{Key: "prop4", Value: 1}, // prop4 asc
			{Key: "prop5", Value: 1}, // prop5 asc
		},
	},
})
if err != nil {
	log.Printf("Failed to create indices: %v", err)
}

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:

确定