我想使用]对字段求和
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 * ...
首先,Collection.Aggregate()
返回一个Collection.Aggregate()
,而不是“直接”结果。您必须遍历光标以获取结果文档(例如,使用mongo.Cursor
和mongo.Cursor
),或使用Cursor.Next()
一步来获取所有结果文档。