我正在尝试对集合进行全文搜索,但为了做到这一点,我需要创建一个文本索引。如何在两个字段上创建文本索引?
我知道我必须使用这样的东西:
opts := options.CreateIndexes().SetMaxTime(10 * time.Second)
idxFiles := []mongo.IndexModel{
{
Keys: bsonx.Doc{{"name": "text"}},
},
}
db.Collection("mycollection").Indexes().CreateMany(context, idx, opts)
Run Code Online (Sandbox Code Playgroud)
我已经找到了解决方案:
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)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10518 次 |
| 最近记录: |