These are chat archives for Automattic/mongoose

20th
Jun 2018
Dhawaldeep
@Dhawaldeep
Jun 20 2018 17:20

Hi
Can anyone help me with Model.geoSearch.
My problem is as follows
I am using Mongoose version 5.1.5, Mongodb version 3.0.10 When I use mongoose Model.geoSearch method, it gives me an error saying MongoError: No geoSearch Index Below is the schema

var hotelSchema = new mongoose.Schema({
name:   {
    type: String,
    required: true
},
stars:  {
    type: Number,
    min: 0,
    max: 5,
    "default": 0
},
services:   [String],
description: String,
photos: [String],
currency : String,
reviews : [reviewSchema],
rooms : [roomSchema],
location : {
  address : String,
  // Always store coordinates longitude (East/West), latitude (North/South) 
  order.
  coordinates : {
    type : [Number],
    index: '2dsphere'
  }
}
});

Below is the geoSearch method

var geoOptions = {
    near: [lng,lat],
    maxDistance: 2000,
    limit: 5
}
Hotel
    .geoSearch({coordinates: '2dsphere'}, geoOptions, function(err, results, stats){
        if(err){
            console.log('Error: '+err);
            res
                .json(err);
        } else{
            console.log("Geo Results " + results);
            console.log("Geo Stats "+ stats);
            res
                .status(200)
                .json(results);
        }

    });

And here is what appears in the console

Error: MongoError: no geoSearch index

As it says there is no geoSearch index thus I have added a 2dsphere index which can be seen in the schema.
And below is what appears when I getindexes()

    [
     {
            "v" : 2,
            "key" : {
                    "_id" : 1
            },
            "name" : "_id_",
            "ns" : "meanhotel.hotels"
    },
    {
            "v" : 2,
            "key" : {
                    "location.coordinates" : "2dsphere"
            },
            "name" : "location.coordinates_2dsphere",
            "ns" : "meanhotel.hotels",
            "background" : true,
            "2dsphereIndexVersion" : 3
    },
    {
            "v" : 2,
            "key" : {
                    "location" : 1
            },
            "name" : "location_1",
            "ns" : "meanhotel.hotels",
            "background" : true
    },
    {
            "v" : 2,
            "key" : {
                    "coordinates" : 1
            },
            "name" : "coordinates_1",
            "ns" : "meanhotel.hotels",
            "background" : true
    },
    {
            "v" : 2,
            "key" : {
                    "coordinates" : "2dsphere"
            },
            "name" : "coordinates_2dsphere",
            "ns" : "meanhotel.hotels",
            "background" : true,
            "2dsphereIndexVersion" : 3
    }
 ]
Kev
@lineus
Jun 20 2018 18:04
@Dhawaldeep you're putting the 2dsphere index on the coordinates field of location, when I believe it should be on the location field. check out the 2dshpere index docs here
Dhawaldeep
@Dhawaldeep
Jun 20 2018 18:05
ok and what should I put in the condition param of geoSearch method??
@lineus when I put 2dsphere index on location, the console says Error: cyclic dependency detected
Kev
@lineus
Jun 20 2018 18:19
hmm, I don't have much experience with geospatial queries. But I think there's more than where the index was created going on here. Your location field doesn't confrom to the getJSON spec, it should be { type: String, coordinates: Array }
Dhawaldeep
@Dhawaldeep
Jun 20 2018 18:20
@lineus ok thanks for the response
Kev
@lineus
Jun 20 2018 18:20
removed the last part as it was specific to Polygon which isn't what you need here
Dhawaldeep
@Dhawaldeep
Jun 20 2018 18:20
yes
sure
let me know if you don't get it working, I'm happy to dig in further :smile: I need the geospatial workout!
Dhawaldeep
@Dhawaldeep
Jun 20 2018 18:21
sure
I am doing a MEAN tutorial, but as it is an old one so the method (geoNear in mongoose) which the author used is now deprecated
Kev
@lineus
Jun 20 2018 19:12
fwiw, Val has a blog post here that's got some good examples on it.
Dhawaldeep
@Dhawaldeep
Jun 20 2018 19:20
@lineus yes I have seen this earlier also but they are using the native MongoDB driver not the mongoose driver
Kev
@lineus
Jun 20 2018 19:22
the only thing that effects is having the db.collection before the command instead of model. all of those commands work in mongoose.
except for .pretty(), that's not part of mongose :smile:
Dhawaldeep
@Dhawaldeep
Jun 20 2018 19:27
oh ok