亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 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 關注
  • 428 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號