基于https://github.com/simagix/mongo-go-examples/blob/master/examples/aggregate_array_test.go上的示例,我创建了下面的代码,该代码在集合上运行管道:
pipeline := `[
{
$group: {
_id: {
year: { $year: "$action_date" },
month: { $month: "$action_date" },
day: { $dayOfMonth: "$action_date" },
},
count: { $sum: 1 }
}
}
]`
var doc bson.M
var cur *mongo.Cursor
collection := apiContext.MongoClient.Database(apiContext.DatabaseName).Collection(util.ActionCollection)
opts := options.Aggregate()
if cur, err = collection.Aggregate(ctx, MongoPipeline(pipeline), opts); err != nil {
}
defer cur.Close(ctx)
for cur.Next(ctx) {
cur.Decode(&doc)
fmt.Println(doc["count"])
}
我没有收到任何错误,但是输出不是我期望的。我没有根据日期返回文档数,而是获得了集合中的所有文档。
我不知道自己缺少什么,但我不应该得到分组的结果吗?
答案 0 :(得分:1)
我不知道自己缺少什么,但我不应该得到分组的结果吗?
这里没有什么要指出的。该代码示例使用MongoPipeline的github.com/simagix/keyhole/mdb
模块。该函数应该采用您的字符串格式的MongoDB管道$group
并转换为bson。 mongo-go-driver使用此格式发送给MongoDB服务器。
不幸的是,MongoPipeline
函数没有返回您的代码可能捕获的任何error
,但是该函数正在使用encoding/json
模块将管道转换为bson
,并且在那里是错误。您收到的JSON解析错误是:
invalid character '$' looking for beginning of object key string
=>因为密钥需要用"
包裹invalid character '}' looking for beginning of object key string
=>因为}
后还有多余的逗号。 由于这些错误,MongoPipeline
返回空数组[]
,这意味着未定义的聚合管道,并将返回集合中的所有文档。
正确的管道JSON字符串是(您可以使用jsonlint.com进行验证)
[{
"$group": {
"_id": {
"year": {
"$year": "$action_date"
},
"month": {
"$month": "$action_date"
},
"day": {
"$dayOfMonth": "$action_date"
}
},
"count": {
"$sum": 1
}
}
}]
根据您的用例,您可以跳过keyhole/mdb
和encoding/json
模块,而直接使用bson
。例如:
groupStage := `{
"$group": {
"_id": {
"year": { "$year": "$action_date" },
"month": { "$month": "$action_date" },
"day": { "$dayOfMonth": "$action_date" }
},
"count": { "$sum": 1 }
}
}`
var group bson.M
err = bson.UnmarshalExtJSON([]byte(groupStage), false, &group)
if err!=nil {
fmt.Println(err)
}
var doc bson.M
// You can add another stage i.e. bson.A{group, sort, project}
cursor, err := collection.Aggregate(context.Background(), bson.A{group})
defer cursor.Close(context.Background())
if err!=nil {
log.Fatal(err)
}
或者,您也可以将mongo.Pipeline与bson.M或bson.D直接使用:
pipeline := mongo.Pipeline{
{{"$group", bson.D{
{"_id", bson.D{
{"year", bson.D{{"$year", "$action_date"}}},
{"month", bson.D{{"$month", "$action_date"}}},
{"day", bson.D{{"$dayOfMonth", "$action_date"}}}
}},
{"count", bson.D{{"$sum", 1}}},
}}},
}
上面的示例是用当前稳定的MongoDB Go driver v1.1.3。编写的。