These are chat archives for Automattic/mongoose

21st
Jun 2018
Dhawaldeep
@Dhawaldeep
Jun 21 2018 14:49

@lineus Hi Now I used the following method but it's not working and gives an error

Hotel.find({
        "location.coordinates": {
            $near: {
                $geometry: {
                    type: 'Point',
                    coordinates: [lng ,lat]
                }, 
                $maxDistance: 2000
            }
        }
    }, function(err, results){
        if(err){
            console.log('Error: '+err);
            res
                .json(err);
        } else{
            console.log("Geo Results " + results);
            res
                .status(200)
                .json(results);
        }
    })

But the console responds with the following error

Error: CastError: Cast to number failed for value "NaN" at path "location.coordinates" for model "Hotel"

Do you or anyone else can help me to get past this ?
Please any any suggestion would mean a lot to me!!

Kev
@lineus
Jun 21 2018 15:12
@Dhawaldeep what do you get when you query against location instead of location.coordinates?
Dhawaldeep
@Dhawaldeep
Jun 21 2018 15:15
same cast error
but instead of location.coordinates I get _QueryString_\
something like this
Kev
@lineus
Jun 21 2018 15:15
what does your schema look like now?
Dhawaldeep
@Dhawaldeep
Jun 21 2018 16:42
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 : {
      type: {
        type: String,
        default: 'Point'
      },
      address : String,
      // Always store coordinates longitude (East/West), latitude (North/South) order.
      coordinates : {
        type : Array
      }
    }
});

hotelSchema.index({coordinates: '2dsphere'});

mongoose.model('Hotel', hotelSchema, 'hotels');