2

I'm using Mongoose with Mongoose-geojson-schema however I cannot add 2dsphere index on my field:

new Schema({
  district: {
    type: String,
    trim: true,
    unique: true,
    required: true
  },
  area: {
    type: GeoJSON.FeatureCollection,
    index: '2dsphere'
  }
});

Getting such error:

/Users/dmitri/api/node_modules/mongoose/lib/schema.js:479
   throw new TypeError('Undefined type `' + name + '` at `' + path +
      ^
TypeError: Undefined type `2dsphere` at `area.index`
   Did you try nesting Schemas? You can only nest using refs or arrays.
Kosmetika
  • 20,774
  • 37
  • 108
  • 172

1 Answers1

0

I think you cannot use Mongoose-geojson-schema that way, it messes up the 'type' attributes - try this:

var mySchema = new Schema({
    district: {
        type: String,
        trim: true,
        unique: true,
        required: true
    },
    area: GeoJSON.FeatureCollection
 });

 mySchema.path('area').index({ type: '2dsphere'});
Reto
  • 3,107
  • 1
  • 21
  • 33