Not getting results with $near/$geoNear - GeoJSON in nested location object

Viewed 151

I'm trying to filter based on $near/$geoNear provide by MongoDB and have Apollo GraphQL backend. But unable to do it.

Here is my object:

{
  firstName:"Steve",
  lastName:"Jobs",
  location:{
    name:"Cupertino, CA",
    geoCords:{
      type: "Point",
      coordinates: [72.0000,54.9999]
    },
    lat: 54.9999,
    lng: 72.0000
  }
}

Here as you can see geoCords is the GeoJSON object.

Quick Question/Clarification:

Is GeoJSON, a regular JSON object that should have type(String) and coordinates(Array [lng, lat]) ? Or do we need to insert GeoJSON in a different way?


How am I setting the index?

I have set the index in the following way:

db.collection.createIndex({ geoCords: '2dsphere' }); also have tried db.collection.createIndex({ 'location.geoCords': '2dsphere' });

When I try to query it/find it, I'm not getting the results.

Here is my query:

db.collection.find({
 location.geoCords: {
   $near:{
     $geometry:{
      type:'Point',
      coordinates: [72.0,56.0]
     }
   }
 }
})
0 Answers
Related