import "go.mongodb.org/mongo-driver/bson"
import "go.mongodb.org/mongo-driver/bson/primitive"
import "go.mongodb.org/mongo-driver/mongo"
import "go.mongodb.org/mongo-driver/mongo/options"
func count() int{
collection := client.Database("thepolyglotdeveloper").Collection("people")
filter := bson.M{"sig": bson.M{ "$eq": "sdfsdf" }}//iv.Sig
cur,_ := collection.Find(context.TODO(), filter)
count,_ := collection.Count(context.TODO(), filter)
return count
}
func main(){
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")
client, _ = mongo.Connect(ctx, clientOptions)
.....
}收到的错误:-
# command-line-arguments
./example.go:131:23: collection.Count undefined (type *mongo.Collection has no field or method Count)我相信我没有使用正确的计数方法。我正在使用它来找出在数据库中是否有任何记录预先存在于过滤器中。请提个建议。
count,_ := collection.Count(context.TODO(), filter)这是正确的用法吗?
发布于 2019-09-30 13:04:21
EstimatedDocumentCount和CountDocuments是用于获取集合中文档数的方法。
请参阅MongoDB Go驱动程序的一般文档,了解可用功能:
https://stackoverflow.com/questions/58155223
复制相似问题