I have an array of locations in a document and I want to add a 2dSpere index on that array. Is that possible?
var LocationSchema = new Schema({
type: { type: String, required: true },
geometry: {
type: { type: String, required: true },
coordinates: { type: Array, required: true}
},
properties: Schema.Types.Mixed
});
// Collection to hold users
var UserSchema = new Schema({
username: { type: String, required: true, unique: true },
locations: [LocationSchema]
},{
versionKey: false
}
);
How do I add a 2DSphere index on the geometry field in the locations array?
https://github.com/LearnBoost/mongoose/blob/master/examples/geospatial/geoJSONSchema.js#L19
LocationSchema.index({ 'geometry' : '2dsphere' });
If you store values for location as 2 numbers in array, then your index would look like:
coordinates: { type: [Number], index: '2dsphere', required: true }
Although you need to create such index in your database:
db.users.ensureIndex({ 'locations.geometry.coordinates': '2dsphere' });