Marc Rasmussen
Marc Rasmussen

Reputation: 20545

Mongoose throws badvalue on coordinates

I have the following schema:

module.exports = function (mongoose) {
    var Location = mongoose.Schema({
        street: String,
        number: String,
        zip: String,
        floor: String,
        rental: String,
        size: Number,
        loc: {
            type: {type: String},
            coordinates: []
        },
        updated: {type: Date, default: Date.now}
    });

    return mongoose.model('Location', Location);
};

i am trying to query the data the following way:

mongoose.models.Location.find({
    loc: {
        $near: [55.69841959999999,12.5443359],
        $maxDistance: 2000
    }
}, function (err, doc) {
    res.json(doc);
});

However i get an error saying:

> error processing query: ns=RentalDB.locationsTree: GEONEAR  field=loc maxdist=2000 isNearSphere=0
Sort: {}
Proj: {}
 planner returned error: unable to find index for $geoNear query

Can anyone tell me what ive done wrong?

Upvotes: 0

Views: 402

Answers (2)

Akarsh Satija
Akarsh Satija

Reputation: 1805

Your geoData should be in correct format while storing.

Coordinates are numbers not string

Refer: https://docs.mongodb.com/manual/geospatial-queries/#legacy-coordinate-pairs

Upvotes: 1

Orelsanpls
Orelsanpls

Reputation: 23495

Doumentation about geoNear

Returns documents in order of proximity to a specified point, from the nearest to farthest. geoNear requires a geospatial index.

You need to create a geospatial index.

Upvotes: 0

Related Questions