Reputation: 5782
I try to create a 2dsphere index in my collection, when i create a user that i should has a 2dsphere index.
I take a reference from this answer Create & Find GeoLocation in mongoose
I use the code is this in my case:
DriverSchema.index({ "geometry": "2dsphere"});
But when i create a user, i don't see any 2dsphere index.
I don't know which step was wrong, wish some one can tell me.
Here is my schema code:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const PointSchema = new Schema({
type: { type: String, default: 'Point'},
coordinates: { type: [Number], index: '2dsphere'}
});
const DriverSchema = new Schema({
email: {
type: String,
required: true
},
driving: {
type: Boolean,
default: false
},
geometry: PointSchema
});
DriverSchema.index({ "geometry": "2dsphere"});
const Driver = mongoose.model('driver', DriverSchema);
module.exports = Driver;
Thanks in advance.
Upvotes: 1
Views: 2026
Reputation: 2111
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")
}
}
Upvotes: 2