I keep getting the "unable to find index for $geoNear query" error. I'm using nestjs and mongoose. Below is the original code when I first got the error. I then tried adding "this.locationModel.createIndexes({ location: "2dsphere" });" (mongoose version of createIndex) That didn't work either. Same error.
import { Injectable } from "@nestjs/common";
import { InjectModel } from "@nestjs/mongoose";
import { Model } from "mongoose";
import { Location } from "src/types/location";
@Injectable()
export class LocationService {
constructor(
@InjectModel("Location") private locationModel: Model<Location>
) {}
async getNearbyLocations(lon, lat, pageSize, pageNum) {
const locations = await this.locationModel
.find({
location: {
$near: {
$geometry: { type: "Point", coordinates: [lon, lat] },
$minDistance: 0,
$maxDistance: 20000,
},
},
})
return locations;
}
Here is the location Document file
import { Document } from "mongoose";
export interface Location extends Document {
description: String;
city: String;
state: String;
location: {
type: String;
coordinates: Array<Number>;
};
}