将提示与 CountDocument 一起使用时,MongoDB golang 驱动程序不会返回无法识别的字段 `hint`

标签 mongodb go

因此,我正在为 go go.mongodb.org/mongo-driver 使用官方的 mongo 驱动程序,并且在容器中运行 mongodb v4。所有操作都运行良好,但当我尝试向 CountDocument 添加提示时,它失败并显示错误提示 unrecognized field hint。我在旧的 gh 问题和 jira 线程中看到了这个错误,但在这些情况下的问题是人们使用的 mongo 版本早于 3.6,但我使用的是 v4,所以我无法找出问题所在。

opts := &options.CountOptions{}
opts.SetHint("client_1_created_at_1")

count, err := r.collection.CountDocuments(ctx, query, opts)
if err != nil {
    return 0, err
}
return count, nil

MongoDB 版本:4.0.6 Mongo驱动版本:1.0.1

最佳答案

mongo-go-driver 中的

CountDocuments() 遵循 mongo shell countDocuments() 的做法,它是 $ 的 aggregate匹配$group。您将无法在 aggregate 上使用提示。

查看实现 here . SetHint() 选项具有误导性,我认为它仍然存在错误。

旁注:应该信任查询规划器,只要您有合适的索引,mongo 查询规划器就会为您选择正确的索引。

关于将提示与 CountDocument 一起使用时,MongoDB golang 驱动程序不会返回无法识别的字段 `hint`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56684987/

相关文章:

Golang 结构的 XML 和 JSON 标签?

go - 3 秒后关闭 SSH session

database - MongoDb 可线性化读取关注限制

python - 更新 'username' 字段将用户注销(flask-login - MongoDB) - 类问题?

MongoDB/PyMongo 地理空间查询 : distance of documents from a point

xml - 使用命名空间迭代 xml 中的元素

javascript - GO - html 文件无法加载外部 js 文件

pointers - Golang 结构问题指向父结构方法

node.js - Mongoose 模式和集合

mongodb - 在使用 mongodump 创建转储之前,是否有必要在 MongoDB 中执行 fsyncLock?