猫鼬错误没有geoNear的地理索引

时间:2020-01-10 05:51:12

标签: javascript node.js reactjs express mongoose

[![在此处输入图像描述] [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

1 个答案:

答案 0 :(得分:0)

以供将来参考。我通过删除嵌套并使文档更平整并仅添加了与同一访问级别上的位置键分开的另一个键来解决此问题。现在一切正常。

  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;