英文:
MGO : Can't canonicalize query: BadValue unknown operator: $meta
问题
我正在使用MGO与MongoDB进行通信。
我想在一个集合中进行搜索,并按搜索得分对结果进行排序。
collection.Find(bson.M{
"$text": bson.M{"$search": "mysearch"},
"score": bson.M{"$meta": "textScore"},
})
但是我得到了这个错误:
无法规范化查询:BadValue未知运算符:$meta(状态码:500)
当我只使用$text
时,它可以工作。
我使用的bson结构与这里相同:
https://stackoverflow.com/questions/23639896/mongodb-cant-canonicalize-query-badvalue-unknown-operator-meta?rq=1
谢谢。
英文:
I am using the MGO to communicate with mongodb.
I want to search in a collection, and to sort the results by search score.
collection.Find(bson.M{
"$text": bson.M{"$search": "mysearch"},
"score": bson.M{"$meta": "textScore"},
})
But I get this error :
Can't canonicalize query: BadValue unknown operator: $meta (status code : 500)
When I only try with $text, it works.
I use the same bson structure than here :
https://stackoverflow.com/questions/23639896/mongodb-cant-canonicalize-query-badvalue-unknown-operator-meta?rq=1
Thanks
答案1
得分: 2
问题在于你将 BSON "map" 作为参数与 "projection" 一起作为 "query" 使用。而应该在投影中使用 .Select()
方法进行链式调用:
collection.Find(
bson.M{"$text": bson.M{"$search": "mysearch"}}
).Select(
bson.M{"score": bson.M{"$meta": "textScore"}}
)
就像标准 API 示例中一样,"query" 和 "projection" 部分是相互"分离"的。
.Select()
方法的文档可以在这里找到。
英文:
The problem here is that you are using a BSON "map" as the argument with the "projection" as part of the "query". Instead use the .Select()
method in chaining for projection:
collection.Find(
bson.M{ "$text": bson.M{ "$search": "mysearch" } }
).Select(
bson.M{ "score": bson.M{ "$meta": "textScore" } }
)
Just like in the standard API examples, the "query" and "projection" portions are "separated" from each other.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论