【问题标题】:Mongoose - how to add 2dsphere index on custom type?Mongoose - 如何在自定义类型上添加 2dsphere 索引?
【发布时间】:2015-10-20 15:10:45
【问题描述】:

我正在使用带有 Mongoose-geojson-schema 的 Mongoose,但是我无法在我的字段中添加 2dsphere 索引:

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

得到这样的错误:

/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.

【问题讨论】:

    标签: node.js mongodb mongoose geojson 2dsphere


    【解决方案1】:

    我认为你不能那样使用 Mongoose-geojson-schema,它会弄乱“类型”属性 - 试试这个:

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

    【讨论】:

      猜你喜欢
      • 2013-07-03
      • 2021-10-25
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 2011-08-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多