这是我的架构
const UserSchema = new Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
skills: [{
type: String
}],
location: {
type: {
type: String,
index: '2dsphere'
},
coordinates: []
}
});
我的查询看起来像这样
User.find({
location: {
$nearSphere: {
$geometry: {
type: "Point",
coordinates: [77.032448, 28.590654]
},
$maxDistance: 2000
}
}
}).find((error, results) => {
if (error) console.log(error);
console.log(results);
});
每当我运行代码时,我都会收到此错误
ok: 0,
errmsg:
'error processing query: ns=StudyJamm.usersTree: GEONEAR field=location maxdist=2000 isNearSphere=0\nSort: {}\nProj: {}\n planner returned error: unable to find index for $geoNear query',
code: 2,
codeName: 'BadValue',
name: 'MongoError',
[Symbol(mongoErrorContextSymbol)]: {}
PS:-我已经试过了User.index({location: "2dsphere"});
我在这里做错了什么?我已经解决了关于 Stack Overflow 的所有类似问题,但没有任何解决方案适合我。