为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用 golang 和 mongo-go-driver 在 mongodb 中创建文本索引?

如何使用 golang 和 mongo-go-driver 在 mongodb 中创建文本索引?

Go
达令说 2023-06-26 17:20:01
我正在尝试对集合进行全文搜索,但为了做到这一点,我需要创建一个文本索引。如何在两个字段上创建文本索引?我知道我必须使用这样的东西:opts := options.CreateIndexes().SetMaxTime(10 * time.Second)idxFiles := []mongo.IndexModel{    {      Keys: bsonx.Doc{{"name": "text"}},    },  }db.Collection("mycollection").Indexes().CreateMany(context, idx, opts)
查看完整描述

5 回答

?
慕雪6442864

TA贡献1812条经验 获得超5个赞

我已经找到了解决方案:


    coll := db.Collection("test")

    index := []mongo.IndexModel{

        {

            Keys: bsonx.Doc{{Key: "name", Value: bsonx.String("text")}},

        },

        {

            Keys: bsonx.Doc{{Key: "createdAt", Value: bsonx.Int32(-1)}},

        },

    }


    opts := options.CreateIndexes().SetMaxTime(10 * time.Second)

    _, errIndex = coll.Indexes().CreateMany(context, index, opts)

    if err != nil {

        panic(errIndex)

    }


查看完整回答
反对 回复 2023-06-26
?
人到中年有点甜

TA贡献1895条经验 获得超7个赞

我编写了一个函数来MongoDB indexes documentation为 MongoDB 支持的所有类型创建索引。

func AddIndex(dbName string, collection string, indexKeys interface{}) error {

    db := getNewDbClient() // get clients of mongodb connection

    serviceCollection := db.Database(dbName).Collection(collection)

    indexName, err := serviceCollection.Indexes().CreateOne(mtest.Background, mongo.IndexModel{

        Keys: indexKeys,

    })

    if err != nil {

        return err

    }

    fmt.Println(indexName)

    return nil

}

mongodb单字段索引:


AddIndex("mydb", "mycollection", bson.M{"myfieldname": 1}) // to descending set it to -1

mongodb 复合索引:


AddIndex("mydb", "mycollection", bson.D{{"myFirstField", 1},{"mySecondField", -1}}) // to descending set it to -1

mongodb 文本索引


AddIndex("mydb", "mycollection", bson.D{{"myFirstTextField", "text"},{"mySecondTextField", "text"}})



查看完整回答
反对 回复 2023-06-26
?
Smart猫小萌

TA贡献1911条经验 获得超7个赞

要使用官方 mongo go 驱动程序创建索引,您可以使用以下代码:


// create Index

indexName, err := c.Indexes().CreateOne(

        context.Background(),

        mongo.IndexModel{

                Keys: bson.M{

                        "time": 1,

                },

                Options: options.Index().SetUnique(true),

        },

)

if err != nil {

        log.Fatal(err)

}

fmt.Println(indexName)

您可以将其替换为您想要的索引配置。


查看完整回答
反对 回复 2023-06-26
?
慕虎7371278

TA贡献1802条经验 获得超4个赞

您可以使用自定义选项和权重简单地创建文本索引,如下所示:


mod := mongo.IndexModel{

    Keys: bson.D{

        {"title", "text"},

        {"description", "text"},

    },

    // create UniqueIndex option

    Options: options.Index().SetWeights(bson.D{

        {"title", 9},

        {"description", 3},

    }),

}


collection.Indexes().CreateOne(context.Background(), mod)


查看完整回答
反对 回复 2023-06-26
?
德玛西亚99

TA贡献1770条经验 获得超3个赞

具有地理空间 (2dsphere)、文本和单个字段。


models := []mongo.IndexModel{

    {

        Keys: bsonx.Doc{{Key: "username", Value: bsonx.String("text")}},

    },

    {

        Keys: bsonx.Doc{{Key: "NearCoordinates", Value: bsonx.String("2dsphere")}},

    },

    {

        Keys: bsonx.Doc{{Key: "password", Value: bsonx.Int32(1)}},

    },

}

opts := options.CreateIndexes().SetMaxTime(20 * time.Second)

_, err := collectionName.Indexes().CreateMany(context.Background(), models, opts)


查看完整回答
反对 回复 2023-06-26
  • 5 回答
  • 0 关注
  • 220 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信