简体   繁体   中英

Mongoose error no geo indices for geoNear

[![enter image description here][1]][1]Originally I had it divided up into a schemas but have since removed them and simply nested them inside of my overall document. I have tried putting indexes on everything and removing them from coordinates to location. I am thinking there could be an issue because the points are nested. When I run get Indexes it shows that there is an index on location.point. No matter what it keeps saying that they are no indexes. I am new to MERN. I am not sure what I am missing here. How do I fix this error so that GeoNear finds the index in the nested document. As you can see I am using the dot notation and have added the index.

  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

For future reference. I fixed it by removing the nesting and making the document flatter and just adding another key separate from the location key on the same access level. Everything works fine now.

  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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM