2

I have a collection 'place' ,one documentis as below

{ 
"_id" : ObjectId("52401a7267778834a23a54a2"),
"userid" : "123",
"loc" : {
    "lng" : 77.6166685, 
    "lat" : 12.9361732 
},
"t" : ISODate("2013-04-23T10:39:46.540Z") 

}

I want to find out locations of user with userid 234 and loc near to [77.6166685,12.9361732] within 1km

but the below query is not working

db.place.find({{"userid":"234","loc":{"$near":{"$geometry":{"type":"point","coordinates":[77.6166685,12.9361732]},"$maxDistance":1000}}})

it showing error as given below

error: {
    "$err" : "point not in interval of [ -180, 180 ] :: caused by :: { 0: 0.0, 1: 250.0 }",
    "code" : 16433

}

what is this error and how can I correct it?

sajith
  • 2,564
  • 8
  • 39
  • 57

1 Answers1

3

I think that the problem is the following:

You document has a form which is applicable for "2d" indexes, (you have not told what kind of indexes do you have) and you are trying to query it with a query which is applicable for "2dsphere" indexes. So you have two options:

  • convert the document to GeoJSON format (here you are not specifying geometry), put 2dsphere index and to query it in this way
  • do nothing and to query it with a correct way with "2d" indexes

I have done in the second way:

db.d.find({
  "loc":{
      "$near": [77.6166685,12.9361732],
      "$maxDistance":1000
  }
})

and got a correct result.

Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
  • this query returns me result,but I think "$maxDistance":1000 is not working as we expected.it returning result greater distance than 1KM.the doc of $maxDistance only specifies distance in unit not in meter – sajith Dec 04 '13 at 08:12
  • Can you show me the that wrong result (with a distance bigger then 1km). Based on documentation it is in meters. – Salvador Dali Dec 04 '13 at 08:23
  • db.place.find({loc:{$near:[77.6166685,12.9361732],$maxDistance:1000}}).skip(525).limit(1) { "_id" : ObjectId("529a9ccf67778812342a"), "userid" : "234", "loc" : { "lng" : 76.2014665, "lat" : 10.5349755 }, "t" : ISODate("2013-11-01T02:19:59.518Z") } – sajith Dec 04 '13 at 08:37
  • actually this is strange. Right now no idea why does this happen. – Salvador Dali Dec 04 '13 at 08:54
  • may be this is the answer for this http://stackoverflow.com/questions/5319988/how-is-maxdistance-measured-in-mongodb – sajith Dec 04 '13 at 09:44
  • @sajith Exactly, you are right. Here you are not using spherical query and therefore that answer is applicable. – Salvador Dali Dec 04 '13 at 10:43
  • so instead of "$maxDistance":1000 i will use "$maxDistance":0.009 (1/111.12, because approximatly 111.12KM=1 radian) – sajith Dec 04 '13 at 11:24