0

I have a collection with a 2dsphere index and want to use the $geoWithin and $centerSphere functions to query it.

Excerpt of the model:

{
// ...
coordinates: {
    coordinates: {
        type: Array,
        index: '2dsphere'
    }
}
// ...
}

I successfully inserted a bunch of documents into the database and now try to query it via:

Model.find({
    coordinates: {
        coordinates: {
            $geoWithin: {
                $centerSphere : [ [ lng, lat], r ]
            }
        }
    }
})

The problem I am facing is, that the call does not return anything, neither an error, nor data. I tried it in the mongo shell as well as in my node.js application with longitudes and latitudes of objects I can confirm were inserted into the database. I also tried to use $near with a $maxDistance with the same result. I furthermore switched latitude and longitude just to check...and I do convert r to radians by dividing by the earth's radius.

I am out of ideas on what goes wrong at this point and thankful for any suggestions!

Haensl
  • 343
  • 3
  • 16
  • Try "coordinates.coordinates" rather than nesting objects. Not sure why you would structure this way anyhhow. But if you are actually not showing your full schema than this is a good reason why this would fail. Also., though not required for `$geoWithin`, have you actually defined a "2dsphere" index? For the `$geoWithin` operator you do not need it (but it helps), but for `$near` or similar you do. – Blakes Seven Jul 17 '15 at 15:52
  • Thank you for your comment. As shown in the Model-excerpt above I did define a `2dsphere` index and it is created successfully, which I proved by checking `db.Model.getIndexes(): { "v" : 1, "key" : { "coordinates.coordinates" : "2dsphere" }, "name" : "coordinates.coordinates_2dsphere", "ns" : "myDb.myCollection", "background" : true, "safe" : null, "2dsphereIndexVersion" : 2 }` I also tried querying via `coordinates.coordinates` which results in `E QUERY SyntaxError: Unexpected token .`. – Haensl Jul 17 '15 at 15:56
  • Umm. Quotes. therefore literally `"coordinates.coordinates"`. it's called ["dot notation"](http://docs.mongodb.org/manual/core/document/#dot-notation) and how you explicitly reference individual fields in a MongoDB document within nested levels. I already said the index was not required. – Blakes Seven Jul 17 '15 at 16:03
  • @BlakesSeven you are king, mate! Thank you, thank you, thank you! It actually was the missing quotes. Problem solved, geospatial queries working. If you write it down as an answer, I can accept it as solving my problem. – Haensl Jul 17 '15 at 16:07
  • I'm sure you mean Queen! Since you looked at my profile, didn't you. Chicks live here too. – Blakes Seven Jul 17 '15 at 16:11

1 Answers1

0

I think you should change your model but, use "dot notation" instead:

Model.find({
    "coordinates.coordinates": {
        "$geoWithin": {
            "$centerSphere" : [ [ lng, lat], r ]
        }            
    }
},function(err,callback) {

});
Blakes Seven
  • 49,422
  • 14
  • 129
  • 135
  • I understand that having a field `coordinates` within a field `coordinates` seems odd, but this is due to third party api conformity. I could refrain from using the outer `coordinates` field, but then I'd have to add it to the results later, when passing them to the client. Simply because it is expected to look like this. – Haensl Jul 17 '15 at 16:12