doulu8847 2017-03-06 07:20
浏览 71
已采纳

Golang-mgo中的$ literal用法

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

  • 写回答

1条回答 默认 最新

  • dotj78335 2017-03-06 11:08
    关注

    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}}}}},
    })
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?