Golang / MongoDB 组聚合返回零值
Golang / MongoDB Group Aggregate returning zero values
我正在尝试获取 MongoDB table 中包含的唯一日期和年份的列表,当我返回正确数量的值时 - 所有内容均为零.
简而言之,我期待:
{ 12 1 },{ 1 6},{ 1 7}
但我得到:
{ 0 0 },{ 0 0 },{ 0 0 }
我错过了什么?
代码和其他信息:
以下是我的MongoDBtable:
{
"_id" : ObjectId("5fecc811698418c18231af40"),
"year" : 2020,
"month" : 12,
"day" : 30,
}
我用来包含数据的结构:
type Test struct {
Year int `bson:"year"`
Month int `bson:"month"`
}
最后,我的代码:
func getEntryDatesHandler(w http.ResponseWriter, r *http.Request) {
pipelineResult := make([]Test, 0)
pipeline := make([]bson.M, 0)
groupStage := bson.M{
"$group": bson.M{
"_id": bson.M{
"year": "$year",
"month": "$month",
},
},
}
pipeline = append(pipeline, groupStage)
data, err := collection.Aggregate(context.Background(), pipeline)
if err != nil {
log.Println("error: ", err.Error())
fmt.Errorf("failed to execute aggregation %s", err.Error())
return
}
err = data.All(context.Background(), &pipelineResult)
if err != nil {
log.Println(err.Error())
fmt.Errorf("failed to decode results", err.Error())
return
}
fmt.Println(pipelineResult)
}
我已验证以下查询使用 Robo 3T 有效,但我尝试直接转换它以 { 0 0 } 作为返回值结束。
db.getCollection('entries').aggregate([
{$group: {
_id: null,
year: {$addToSet: '$year'},
mont: {$addToSet: '$month'}
}}
])
提前致谢!
您的聚合转换文档,将它们分组,然后将年和月字段分组到 _id
。您的 Test
不再对结果文档建模。
您的结果可能会使用 Result
结构建模,如下所示:
type Test struct {
Year int `bson:"year"`
Month int `bson:"month"`
}
type Result struct {
ID Test `bson:"_id"`
}
并使用一片 Result
作为结果:
pipelineResult := make([]Result, 0)
注意:如果您不知道结果文档的结构,您可以始终使用bson.M
对其进行建模和检查:
pipelineResult := make([]bson.M, 0)
我正在尝试获取 MongoDB table 中包含的唯一日期和年份的列表,当我返回正确数量的值时 - 所有内容均为零.
简而言之,我期待:
{ 12 1 },{ 1 6},{ 1 7}
但我得到:
{ 0 0 },{ 0 0 },{ 0 0 }
我错过了什么?
代码和其他信息:
以下是我的MongoDBtable:
{
"_id" : ObjectId("5fecc811698418c18231af40"),
"year" : 2020,
"month" : 12,
"day" : 30,
}
我用来包含数据的结构:
type Test struct {
Year int `bson:"year"`
Month int `bson:"month"`
}
最后,我的代码:
func getEntryDatesHandler(w http.ResponseWriter, r *http.Request) {
pipelineResult := make([]Test, 0)
pipeline := make([]bson.M, 0)
groupStage := bson.M{
"$group": bson.M{
"_id": bson.M{
"year": "$year",
"month": "$month",
},
},
}
pipeline = append(pipeline, groupStage)
data, err := collection.Aggregate(context.Background(), pipeline)
if err != nil {
log.Println("error: ", err.Error())
fmt.Errorf("failed to execute aggregation %s", err.Error())
return
}
err = data.All(context.Background(), &pipelineResult)
if err != nil {
log.Println(err.Error())
fmt.Errorf("failed to decode results", err.Error())
return
}
fmt.Println(pipelineResult)
}
我已验证以下查询使用 Robo 3T 有效,但我尝试直接转换它以 { 0 0 } 作为返回值结束。
db.getCollection('entries').aggregate([
{$group: {
_id: null,
year: {$addToSet: '$year'},
mont: {$addToSet: '$month'}
}}
])
提前致谢!
您的聚合转换文档,将它们分组,然后将年和月字段分组到 _id
。您的 Test
不再对结果文档建模。
您的结果可能会使用 Result
结构建模,如下所示:
type Test struct {
Year int `bson:"year"`
Month int `bson:"month"`
}
type Result struct {
ID Test `bson:"_id"`
}
并使用一片 Result
作为结果:
pipelineResult := make([]Result, 0)
注意:如果您不知道结果文档的结构,您可以始终使用bson.M
对其进行建模和检查:
pipelineResult := make([]bson.M, 0)