How to create geo index in mongoose How to create geo index in mongoose mongoose mongoose

How to create geo index in mongoose


You need to update your schema as below:

coordinates: {  type: [Number], // <lng, lat>  index: { type: '2dsphere', sparse: false },  required: true,},

After this create a new record and check in Robo 3T like this:

db.hotspot.aggregate([{$indexStats: {}}]);

And then you will get this output:

{    "name" : "location_2dsphere",    "key" : {        "coordinates" : "2dsphere"    },    "host" : "neel-Vostro-1450:27017",    "accesses" : {        "ops" : NumberLong(0),        "since" : ISODate("2018-03-11T14:04:12.785Z")    }}