在 mongodb 中使用 golang 进行聚合和过滤的 InvalidPipelineOperator
InvalidPipelineOperator for aggregation and filtering in mongodb with golang
我有一个 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 以使用 mongodb 在 golang 中的聚合和过滤功能来获取特定时间范围内的所有位置。
例如:我想检索从 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")
}
}
为此,我参考了以下两个网页来制作聚合和过滤管道:
起初我在使用github的mdb.MongoPipeline(pipeline)时遇到了麻烦,所以我决定使用github的pipeline格式,并以如下形式使用mongo.Pipeline{...}
的堆栈溢出的第二个答案
生成的管道变为:
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.establish: new Date(1580122004573) }, $gtd: { $$locations.establish : 新日期(1578826004573) } }
然后当尝试通过测试不同的管道案例进行调试时,此管道会导致另一个问题:
...
{"as", "locations"},
{"cond", bson.D{
{"$lte", bson.D{{"$$locations.establish", r.URL.Query()["rangeend"][0]}}},
}},
}},
...
错误是:
(InvalidPipelineOperator) 无法识别的表达式“$$locations.establish”
知道为什么会发生这两个错误吗?以及如何解决?
谢谢。
Any ideas why these two error happen?
由于缺少数组运算符,您会收到这些错误。运算符 $and
需要一个数组,应表示为 bson.A。
And how to fix it?
请参阅以下管道:
// 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)
的当前版本编写的
我有一个 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 以使用 mongodb 在 golang 中的聚合和过滤功能来获取特定时间范围内的所有位置。
例如:我想检索从 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")
}
}
为此,我参考了以下两个网页来制作聚合和过滤管道:
起初我在使用github的mdb.MongoPipeline(pipeline)时遇到了麻烦,所以我决定使用github的pipeline格式,并以如下形式使用mongo.Pipeline{...}
的堆栈溢出的第二个答案生成的管道变为:
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.establish: new Date(1580122004573) }, $gtd: { $$locations.establish : 新日期(1578826004573) } }
然后当尝试通过测试不同的管道案例进行调试时,此管道会导致另一个问题:
...
{"as", "locations"},
{"cond", bson.D{
{"$lte", bson.D{{"$$locations.establish", r.URL.Query()["rangeend"][0]}}},
}},
}},
...
错误是: (InvalidPipelineOperator) 无法识别的表达式“$$locations.establish”
知道为什么会发生这两个错误吗?以及如何解决? 谢谢。
Any ideas why these two error happen?
由于缺少数组运算符,您会收到这些错误。运算符 $and
需要一个数组,应表示为 bson.A。
And how to fix it?
请参阅以下管道:
// 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)
的当前版本编写的