$literal 在 Golang-mgo 中的用法
$literal usage in Golang-mgo
我不明白如何正确使用 $literal。我正在使用 mgo.v2 和 mgo.v2/bson 包。
db.store.aggregate([
{"$project":{
"location":{
"type":{"$literal":"Point"},
"coordinates":["$longitude","$latitude"]
}}
},])
我使用上面的代码在 mongodb 中获取数据,然后 fine.It 得到了结果
{ "location":{
"type":"Point",
"coordinates":[77.587073,12.958794]
}}
我尝试在 golang 中使用相同的,如下所示
pipe :=DB.C("store").Pipe([]bson.M{
{"$project":bson.M{"location":
bson.M{"type":
bson.M{"$literal":"Point"},"coordinates":[]interface{}{"$longitude","$latitude"}}}}}
上面的代码,抛出一个错误
panic: bad query: BadValue: Point must be an array or object
所以我这样替换了它
pipe :=DB.C("store").Pipe([]bson.M{
{"$project":bson.M{"location":
bson.M{"$literal":
bson.M{"type":"Point"},"coordinates":[]interface{}{"$longitude","$latitude"}}}}})
但这也会给我一个错误
panic: this object is already an operator expression, and can't be
used as a document expression (at 'coordinates')
我的完整作品如下link
my work is here
请帮我解决这个问题。
谢谢
为了完整起见,这就是您实际尝试做的事情:
pipe := DB.C("store").Pipe([]bson.M{
{"$project": bson.M{"location": bson.M{"type": bson.M{"$literal": "Point"}, "coordinates": []interface{}{"$longitude", "$latitude"}}}},
{"$match": bson.M{"location": bson.M{"$geoWithin": bson.M{"$centerSphere": []interface{}{"$coordinates", 10 / 6378.11}}}}},
})
问题不在于您的 "Point"
文字,这只是巧合。例如,如果将其更改为 "Pt"
,您仍然会看到完全相同的错误消息。
错误消息中的Point
指的是$centerSphere
,它需要一个中心点和一个半径。而且您尝试 "pass" 的方式不起作用。
这适用于例如:
"$centerSphere": []interface{}{[]interface{}{1.0, 2.0}, 10 / 6378.11}
您的原始查询没有意义,因为您试图查找距离 自身 10 公里以内的文档,这将匹配所有文档。
相反,您想要/应该查询 特定 位置 10 公里以内的文档,您可以将此特定位置的坐标传递给 $centerSphere
:
myLong, myLat := 10.0, 20.0
// ...
"$centerSphere": []interface{}{[]interface{}{myLong, myLat}, 10 / 6378.11}
完整查询:
myLong, myLat := 10.0, 20.0
pipe := DB.C("store").Pipe([]bson.M{
{"$project": bson.M{"location": bson.M{"type": bson.M{"$literal": "Point"}, "coordinates": []interface{}{"$longitude", "$latitude"}}}},
{"$match": bson.M{"location.coordinates": bson.M{"$geoWithin": bson.M{"$centerSphere": []interface{}{[]interface{}{myLong, myLat}, 10 / 6378.11}}}}},
})
我不明白如何正确使用 $literal。我正在使用 mgo.v2 和 mgo.v2/bson 包。
db.store.aggregate([
{"$project":{
"location":{
"type":{"$literal":"Point"},
"coordinates":["$longitude","$latitude"]
}}
},])
我使用上面的代码在 mongodb 中获取数据,然后 fine.It 得到了结果
{ "location":{
"type":"Point",
"coordinates":[77.587073,12.958794]
}}
我尝试在 golang 中使用相同的,如下所示
pipe :=DB.C("store").Pipe([]bson.M{
{"$project":bson.M{"location":
bson.M{"type":
bson.M{"$literal":"Point"},"coordinates":[]interface{}{"$longitude","$latitude"}}}}}
上面的代码,抛出一个错误
panic: bad query: BadValue: Point must be an array or object
所以我这样替换了它
pipe :=DB.C("store").Pipe([]bson.M{
{"$project":bson.M{"location":
bson.M{"$literal":
bson.M{"type":"Point"},"coordinates":[]interface{}{"$longitude","$latitude"}}}}})
但这也会给我一个错误
panic: this object is already an operator expression, and can't be used as a document expression (at 'coordinates')
我的完整作品如下link my work is here 请帮我解决这个问题。 谢谢
为了完整起见,这就是您实际尝试做的事情:
pipe := DB.C("store").Pipe([]bson.M{
{"$project": bson.M{"location": bson.M{"type": bson.M{"$literal": "Point"}, "coordinates": []interface{}{"$longitude", "$latitude"}}}},
{"$match": bson.M{"location": bson.M{"$geoWithin": bson.M{"$centerSphere": []interface{}{"$coordinates", 10 / 6378.11}}}}},
})
问题不在于您的 "Point"
文字,这只是巧合。例如,如果将其更改为 "Pt"
,您仍然会看到完全相同的错误消息。
错误消息中的Point
指的是$centerSphere
,它需要一个中心点和一个半径。而且您尝试 "pass" 的方式不起作用。
这适用于例如:
"$centerSphere": []interface{}{[]interface{}{1.0, 2.0}, 10 / 6378.11}
您的原始查询没有意义,因为您试图查找距离 自身 10 公里以内的文档,这将匹配所有文档。
相反,您想要/应该查询 特定 位置 10 公里以内的文档,您可以将此特定位置的坐标传递给 $centerSphere
:
myLong, myLat := 10.0, 20.0
// ...
"$centerSphere": []interface{}{[]interface{}{myLong, myLat}, 10 / 6378.11}
完整查询:
myLong, myLat := 10.0, 20.0
pipe := DB.C("store").Pipe([]bson.M{
{"$project": bson.M{"location": bson.M{"type": bson.M{"$literal": "Point"}, "coordinates": []interface{}{"$longitude", "$latitude"}}}},
{"$match": bson.M{"location.coordinates": bson.M{"$geoWithin": bson.M{"$centerSphere": []interface{}{[]interface{}{myLong, myLat}, 10 / 6378.11}}}}},
})