MGO: 无法规范化查询:BadValue 未知运算符:$meta

huangapple go评论89阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2015年7月24日 18:18:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/31607964.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定