mongo-go-driver聚合查询始终返回“当前”:null

问题描述 投票:1回答:1

我想使用]对字段求和

pipeline := []bson.M{
    bson.M{"$group": bson.M{
        "_id": "", 
        "count": bson.M{ "$sum": 1}}}
}
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
result, err := collection.Aggregate(ctx, pipeline)

但总是返回

"Current": null

任何解决方案?

我想使用管道求和:= [] bson.M {bson.M {“ $ group”:bson.M {“ _id”:“”,“ count”:bson.M {“ $ sum “:1}}}} ctx,_:= context.WithTimeout(context.Background(),5 * ...

mongodb go mongodb-query aggregation-framework mongo-go
1个回答
0
投票

首先,Collection.Aggregate()返回一个Collection.Aggregate(),而不是“直接”结果。您必须遍历光标以获取结果文档(例如,使用mongo.Cursormongo.Cursor),或使用Cursor.Next()一步来获取所有结果文档。

© www.soinside.com 2019 - 2024. All rights reserved.