我不知道自己缺少什么,但我不应该得到分组的结果吗?
这里没有什么要指出的。该代码示例使用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",{"day",bson.D{{"$dayOfMonth","$action_date"}}}
}},{"count",bson.D{{"$sum",1}}},}}},}
上面的示例是用当前稳定的MongoDB Go driver v1.1.3。编写的。
本文链接:https://www.f2er.com/3122959.html