mongodb - 如何使用 golang 和 mongo-go-driver 在 mongodb 中创建文本索引?
问题描述
我正在尝试对集合进行全文搜索,但为了做到这一点,我需要创建一个文本索引。如何在两个字段上创建文本索引?
我知道我必须使用这样的东西:
opts := options.CreateIndexes().SetMaxTime(10 * time.Second)
idxFiles := []mongo.IndexModel{
{
Keys: bsonx.Doc{{"name": "text"}},
},
}
db.Collection("mycollection").Indexes().CreateMany(context, idx, opts)
解决方案
我已经建立了解决方案:
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)
}
推荐阅读
- javascript - 如何根据角度环境更改 index.html 中的变量值
- python - 在 python 中使用 Lambda 函数优化多个 for 循环
- angularjs - ThreeJs OBJLoader 输出看起来非常像素化
- ruby-on-rails - 如何在rails控制器中使用redirect_to url运行js?
- python - 如何在 Python 中的二进制图像中找到多个质心?
- python - 集成石墨和蝗虫时出错
- python - 如何创建一个 node.js 脚本,该脚本可以单击启动该服务器的外部网站上的按钮
- dynamics-crm - Power App 自定义连接器:无法调用 CRM 函数 RetrieveRolePrivilegesRole
- testing - 如何在不使用滚动和模拟用户活动的情况下从下拉列表中选择一个项目 - 在 TestCafe Studio 中拖动?
- docker - 仅在容器中运行某些部分的应用程序