Mongodb - Error running find geospatial $near

Asked

Viewed 63 times

0

I have a collection with the following documents.

{
    "_id" : ObjectId("5c6bb6b492647b15f80038a1"),
    "loc" : {
        "type" : "Point",
        "coordenadas" : [ 
            -41.104658, 
            -20.963387
        ]
    }
}

I’m running the find:

db.tblfrota.find( 
   { loc : {
        $near :  { 
           $geometry :  { 
              type  :  "Point" , 
              coordenadas  :  [  parseFloat(-41.81064615) ,  parseFloat(-22.39813884)  ] 
           }, 
           $maxDistance :  5000 
        } 
   } 
   }
)

I have the indexes:

[
    {
        "v" : 2,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_"
    },
    {
        "v" : 2,
        "key" : {
            "loc.coordenadas" : "2dsphere"
        },
        "name" : "coord_3d", 
        "2dsphereIndexVersion" : 3
    },
    {
        "v" : 2,
        "key" : {
            "coordenadas" : "2d"
        },
        "name" : "coordenadas_2d"

    }
]

Is returning the error :

Error: error: ? "ok" : 0, "errmsg" : "invalid point in geo near query $Geometry argument: { type: "Point", coordinates: [ -41.81064615, -22.39813884 ] } Point must be an array or Object", "code" : 2, "Codename" : "Badvalue" }

1 answer

0

Solved: the error was in the syntax.

db.tblfrota.find( { Loc.coordinates: { $near : { $Geometry : { type : "Point", coordinates: [ parseFloat(-41.81064615) , parseFloat(-22.39813884) ] }, $maxDistance : 5000 } } } )

Browser other questions tagged

You are not signed in. Login or sign up in order to post.