登录
首页 >  Golang >  Go问答

在 MongoDB 中使用 Golang 进行聚合和筛选时出现问题

来源:stackoverflow

时间:2024-02-23 19:18:23 265浏览 收藏

Golang小白一枚,正在不断学习积累知识,现将学习到的知识记录一下,也是将我的所得分享给大家!而今天这篇文章《在 MongoDB 中使用 Golang 进行聚合和筛选时出现问题》带大家来了解一下##content_title##,希望对大家的知识积累有所帮助,从而弥补自己的不足,助力实战开发!


问题内容

我有一个具有以下结构的 mongodb 集合:

{
        "_id" : objectid("5e2af47006d5b7820876cc34"),
        "uuid" : "6ec5245e-d512-4496-995d-9a7d1073ff80",
        "beaconid" : "fc775907-f442-43ca-9b86-78fede5f9218",
        "locations" : [
                {
                        "longitude" : 464.4,
                        "latitude" : 34.8,
                        "establish" : isodate("2020-01-24t13:43:12.895z")
                },
                {
                        "longitude" : 464.4,
                        "latitude" : 34.8,
                        "establish" : isodate("2020-01-24t13:47:09.066z")
                },
                {
                        "longitude" : 464.4,
                        "latitude" : 34.8,
                        "establish" : isodate("2020-01-24t15:03:02.770z")
                },
                {
                        "longitude" : 464.4,
                        "latitude" : 34.8,
                        "establish" : isodate("2020-01-24t15:23:36.891z")
                }
}
{
     ......
}

我目前正在开发一个 api,以使用 golang 中 mongodb 的聚合和过滤功能来获取特定时间范围内的所有位置。

例如:我想检索从 isodate("2020-01-24t13:45:00.066z") 到 isodate("2020-01-24t15:10:00.770z") 的所有值,这仅检索中间两个位置,输出应如下:

{
        "_id" : objectid("5e2af47006d5b7820876cc34"),
        "uuid" : "6ec5245e-d512-4496-995d-9a7d1073ff80",
        "beaconid" : "fc775907-f442-43ca-9b86-78fede5f9218",
        "locations" : [
                {
                        "longitude" : 464.4,
                        "latitude" : 34.8,
                        "establish" : isodate("2020-01-24t13:47:09.066z")
                },
                {
                        "longitude" : 464.4,
                        "latitude" : 34.8,
                        "establish" : isodate("2020-01-24t15:03:02.770z")
                }
}

为了实现这一点,我参考了以下两个网页来制作聚合和过滤的管道:

https://github.com/simagix/mongo-go-examples/blob/5a04bab8b677e7b160dbe4c327f7ac68efb83ba5/examples/aggregate_reduce_test.go

如何在golang中编写bson形式的mongo查询?

一开始我在使用github的mdb.mongopipeline(pipeline)时遇到了麻烦,所以我决定使用github的pipeline格式,并以mongo.pipeline的堆栈溢出第二个答案的形式使用它{...} p>

最终的管道变为:

new_pipeline = mongo.pipeline{
    {{"$match", bson.d{
         {"beaconid", r.url.query()["beaconid"][0]},
    }}},
    {{"$project", bson.d{
         {"locations", bson.d{
              {"$filter", bson.d{
                  {"input", "$locations"},
                  {"as", "locations"},
                  {"cond", bson.d{
                       {"$and", bson.d{
                           {"$lte", bson.d{{"locations.establish", r.url.query()["rangeend"][0]}}},
                           {"$gtd", bson.d{{"locations.establish", r.url.query()["rangebegin"][0]}}},
                       }},
                  }},
              }},
          }},
     }}},
     {{"$unwind", "$locations"}},
}

opts := options.aggregate()
cursor, err := collection.aggregate(context.todo(), new_pipeline, opts)

但是,运行程序时出现错误,我不知道如何解决:

(location15983) 表示表达式的对象必须只有一个字段: { $lte: { $$locations.assessment: new date(1580122004573) }, $gtd: { $$locations.assessment: new date(1578826004573) } }

然后,当尝试通过测试不同的管道情况进行调试时,该管道会导致另一个问题:

...
{"as", "locations"},
      {"cond", bson.D{
            {"$lte", bson.D{{"$$locations.establish", r.URL.Query()["rangeend"][0]}}},
      }},
}},
...

错误是: (invalidpipelineoperator) 无法识别的表达式 '$$locations.built'

有什么想法为什么会发生这两个错误吗?以及如何修复它? 谢谢。


解决方案


有什么想法为什么会发生这两个错误吗?

由于缺少数组运算符,您会收到这些错误。运算符 $and 需要一个数组,应表示为 bson.A

如何修复它?

请参阅以下管道:

// Create a date object using time.Parse()
lteDate, err := time.Parse(time.RFC3339, "2020-01-24T15:10:00.770Z")
if err!=nil {log.Fatal(err)}
gteDate, err := time.Parse(time.RFC3339, "2020-01-24T13:45:00.066Z")
if err!=nil {log.Fatal(err)}

// MongoDB Aggregation Pipeline Stages
matchStage := bson.D{
    {"$match", bson.D{
        {"beaconid", "fc775907-f442-43ca-9b86-78fede5f9218"},
    }},
}

projectStage := bson.D{
    {"$project", bson.D{
        {"locations", bson.D{
            {"$filter", bson.D{
                {"input", "$locations"}, 
                {"as", "locations"}, 
                {"cond", bson.D{
                    {"$and", bson.A{
                            bson.D{{"$lte", bson.A{"$$locations.establish", lteDate}}},
                            bson.D{{"$gte", bson.A{"$$locations.establish", gteDate}}},
                    }},
                }}, 
            }}, 
        }}, 
    }}, 
}

unwindStage := bson.D{
    {"$unwind", "$locations"},
}
pipeline := mongo.Pipeline{matchStage, projectStage, unwindStage}

上面的代码片段是使用当前版本的 mongo-go-driver (v1.2.x) 编写的

以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>