0

Structure of Document:

enter image description here

Mongoose Schema:

var mongoSchema = new Schema({
name:  String,
description: String,
location: { lat: Number, long: Number },
images: [String],
blocks: { text: String, logo: String },
subLocations: {
    name: String,
    description: String,
    location: {
         lat: Number,
         long: Number
    },
    images: [String]
    }
});

Following Query does not work:

mongooseModel.find().
   where('location.lat').equals(18.710145).
   exec(function(err, response) {
}

While this query does work:

mongooseModel.find().
   where('lat').equals("18.710145").
   exec(function(err, response) {
}

Any guidance?

Aakash Thakkar
  • 301
  • 1
  • 3
  • 20

2 Answers2

0

the datatype of the nested lat (location.lat) is a String (look at your embedded image, far right column). that's why your first query didn't work -- you're comparing a Number to a String.

matias elgart
  • 1,123
  • 12
  • 18
  • But I verified that it is casted as a number due to the mongoose defined schema. Also even after converting it to "18.710145" the query does not return any results. – Aakash Thakkar Sep 19 '17 at 18:43
0

Please try below query to find out your result.

db.myCollection.find( { $where: {"location":{"lat":"18.710145"}} } );
Manish Joshi
  • 93
  • 13