【问题标题】:Mongoose error no geo indices for geoNear猫鼬错误没有geoNear的地理索引
【发布时间】:2020-04-27 18:39:43
【问题描述】:

[![在此处输入图像描述][1]][1]最初我将其划分为一个模式,但后来删除了它们并将它们简单地嵌套在我的整个文档中。我尝试将索引放在所有内容上并将它们从坐标删除到位置。我认为可能存在问题,因为这些点是嵌套的。当我运行 get Indexes 时,它显示 location.point 上有一个索引。 不管它一直说它们不是索引。我是 MERN 的新手。我不确定我在这里缺少什么。如何修复此错误,以便 GeoNear 在嵌套文档中找到索引。如您所见,我使用的是点表示法并添加了索引。

  user: {
    type: Schema.Types.ObjectId,
    ref: "User"
  },
  published: {
      type: Boolean,
      default: false
  },
  placeType: placeTypeSchema,
  capacity: {
    maxGuestSize: Number,
    rooms: Number
  },
  bathrooms: Number,
  location: {
    streetAddress: String,
    suite: String,
    country: String,
    city: String,
    state: String,
    zip: String,
    point: {
      type: {
        type: String,
        default: "Point",
        index: '2dsphere'
      },
      coordinates: {
        type: [Number],
        index: '2dsphere'
        //index: '2dsphere'
      }
    }
  },
  amenities: {
    regular: [String],
    safety: [String]
  },
  sharedSpaces: [String],
  imageUrl: String,
  description: {
    description: String,
    hostAvailablity: String,
    spaceDetails: String,
    neighborhood: String,
    transportation: String
  },
  title: String,
  mobileNumber: String,
  houseRules: houseRulesSchema,
  preference: {
    rentedLocationBefore: Boolean,
    howOftenGuests: String
  },
  notice: {
    guestNoticeTime: Number,
    checkInTime: Date
  },
  advance: Number,
  lengthOfStay: {
    min: Number,
    max: Number
  },
  price: {
    basePrice: Number,
    minPrice: Number,
    maxPrice: Number
  }
});

SpotSchema.index({"location.point": "2dsphere"});


Spot = mongoose.model('Spot', SpotSchema);
module.exports = Spot; ```
[![enter image description here][2]][2]


  [1]: https://i.stack.imgur.com/2hpSE.png
  [2]: https://i.stack.imgur.com/gevTo.png

【问题讨论】:

    标签: javascript node.js reactjs express mongoose


    【解决方案1】:

    供日后参考。我通过删除嵌套并使文档更扁平并在同一访问级别上添加另一个与位置键分开的键来修复它。现在一切正常。

      user: {
        type: Schema.Types.ObjectId,
        ref: "User"
      },
      published: {
          type: Boolean,
          default: false
      },
      placeType: placeTypeSchema,
      capacity: {
        maxGuestSize: Number,
        rooms: Number
      },
      bathrooms: Number,
      location: {
        streetAddress: String,
        suite: String,
        country: String,
        city: String,
        state: String,
        zip: String,
      },
      precise: {
        type: {
          type: String,
          default: "Point"
        },
        coordinates: {
          type: [Number],
          default: [-97.1251805, 33.088988]
          //index: '2dsphere'
        }
      },
      amenities: {
        regular: [String],
        safety: [String]
      },
      sharedSpaces: [String],
      imageUrl: String,
      description: {
        description: String,
        hostAvailablity: String,
        spaceDetails: String,
        neighborhood: String,
        transportation: String
      },
      title: String,
      mobileNumber: String,
      houseRules: houseRulesSchema,
      preference: {
        rentedLocationBefore: Boolean,
        howOftenGuests: String
      },
      notice: {
        guestNoticeTime: Number,
        checkInTime: Date
      },
      advance: Number,
      lengthOfStay: {
        min: Number,
        max: Number
      },
      price: {
        basePrice: Number,
        minPrice: Number,
        maxPrice: Number
      }
    });
    
    SpotSchema.index({"precise": "2dsphere"});
    
    
    Spot = mongoose.model('Spot', SpotSchema);
    module.exports = Spot;
    

    【讨论】:

      猜你喜欢
      • 2012-09-29
      • 2016-05-27
      • 2018-07-22
      • 2018-08-19
      • 1970-01-01
      • 2012-07-30
      • 1970-01-01
      • 2013-12-16
      相关资源
      最近更新 更多