Does applying a 2dsphere index on a mongoose schema force the location field to be required?

后端 未结 4 600
长发绾君心
长发绾君心 2020-12-15 23:17

I have a mongoose schema and model defined as follows:

var mongoose = require(\'mongoose\')
  , Schema = new mongoose.Schema({
      email: {
        index:          


        
4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-15 23:51

    The correct way to create a schema with geolocation and run geospartial queries was

    var UserSchema = new Schema({
      location: {
        type: {
          type: String,
          enum: ["Point"], // 'location.type' must be 'Point'
        },
        coordinates: {
          type: [Number]
        }
      }
    });
    
    UserSchema.index({location: '2dsphere'});
    

    Be careful this is important to do, the index 2dsphere in the location.coordinates, if you want to run geospartial queries!

提交回复
热议问题