$在Golang-mgo中的字面用法 [英] $literal usage in Golang-mgo

查看:163
本文介绍了$在Golang-mgo中的字面用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解如何正确使用$字面值。我使用mgo.v2和mgo.v2 / bson包。

  db.store.aggregate([
{ $ project:{
location:{
type:{$ literal:Point},
coordinates:[$ longitude,$纬度]
}}
},])

代码在mongodb中获取数据并且工作正常。它给了我结果

  {location:{
type:Point,
coordinates:[77.587073,12.958794]
}}

我试图在golang中使用它,它显示在下面

  pipe:= DB.C(管道([] bson.M {
{$ project:bson.M {location:
bson.M {type:
bson.M { $ literal:Point},coordinates:[] interface {} {longitude,$ latitude}}}}}

以上代码引发错误


panic:bad query:BadValue:Point必须是数组或对象


因此,我将其替换为

  pipe:= DB.C(store)。Pipe([] bson.M {
{$ project:bson.M {location:
bson.M { $ literal:
bson.M {type:Point},coordinates:[] interface {} {$ longitude,$ latitude}}}}})

但这也会引发错误


panic:这个对象已经是一个运算符表达式,不能用
作为文档表达式(在'坐标'处)


我的完整作品显示在下面的链接中
我的工作是这里
请帮我解决这个问题。
谢谢

解决方案

为了完整起见,这是您实际尝试做的事:

  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}}}}},
})

code>Point字面意思,这仅仅是一个巧合。例如,如果将其更改为Pt,您仍然会看到完全相同的错误消息。



错误消息中的 Point 指的是 $ centerSphere ,它需要一个中心和一个半径。您尝试传递它的方式不起作用。



例如:

<$ p $
$ centerSphere:[] interface {} {[] interface {} {1.0,2.0},10 / 6378.11} p>

您的原始查询没有意义,因为您尝试查找位置距本身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 {位置: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}}}}},


I can't understand how to properly use $literal. I am using mgo.v2 and mgo.v2/bson package.

db.store.aggregate([
{"$project":{
    "location":{
        "type":{"$literal":"Point"},
        "coordinates":["$longitude","$latitude"]
    }}
},])

I used the above code to fetch data in mongodb and working fine.It gives me the result

 { "location":{
            "type":"Point",
            "coordinates":[77.587073,12.958794] 
        }}

I tried to use the same in golang and it is shown below

pipe :=DB.C("store").Pipe([]bson.M{
        {"$project":bson.M{"location":
        bson.M{"type":
         bson.M{"$literal":"Point"},"coordinates":[]interface{}{"$longitude","$latitude"}}}}}

Above code, throws me an error

panic: bad query: BadValue: Point must be an array or object

so I replaced it like this

pipe :=DB.C("store").Pipe([]bson.M{
        {"$project":bson.M{"location":
        bson.M{"$literal":
         bson.M{"type":"Point"},"coordinates":[]interface{}{"$longitude","$latitude"}}}}})

but this also throws me an error

panic: this object is already an operator expression, and can't be used as a document expression (at 'coordinates')

my complete work is shown in the below link my work is here please help me to solve this. Thank you

解决方案

To be complete, this is what you actually try to do:

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}}}}},
})

The problem is not with your "Point" literal, it's just a mere coincidence. If you change it to "Pt" for example, you will still see the exact same error message.

The Point in the error message refers to $centerSphere, which expects a center point and a radius. And the way you try to "pass" it does not work.

This works for example:

"$centerSphere": []interface{}{[]interface{}{1.0, 2.0}, 10 / 6378.11}

Your original query does not make sense, as you try to find documents where the location is within 10 kilometers from itself, which would match all documents.

Instead you want to / should query documents which are within 10 kilometers of a specific location, and you may pass the coordinates of this specific location to $centerSphere:

myLong, myLat := 10.0, 20.0

// ...

"$centerSphere": []interface{}{[]interface{}{myLong, myLat}, 10 / 6378.11}

The complete query:

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}}}}},
})

这篇关于$在Golang-mgo中的字面用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆