mongoose: Unable to perform geospatial queries

When trying to issue a geospatial query using ‘near’ you get back an error “Can’t use $near”. I have also tried with geoNear and nearSphere as well.

Example:

var Schema = new mongoose.Schema({
    placeName: String,
    geo: {
        type: { type: String, enum: "Point", default: "Point" },
        coordinates: { type: [ Number ],   default: [ 0,0 ] }    
    }
});

somemodel.where('geo').near({ center: [50, 50] }).exec(function(err, locs){
  console.log(locs);
})

The above will return: [Error: Can’t use $near]

I have also tried the following:

model.find({
    geo: {
        $near: {
            $geometry: {
                type: "Point",
                coordinates: [50, 50]
            },
            $maxDistance: maxDistance
        }
    }
}, function(err, locs) {
    console.log(locs);
});

It will also result in the same error: [Error: Can’t use $near]

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 2
  • Comments: 17

Commits related to this issue

Most upvoted comments

Looks like this was just an instance of a missing query operator for single embedded subdocs, much like #4044. Will be fixed when 4.4.14 is released.