因此,我正在为 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/