使用此架构:
let mongoose = require("mongoose");
let GeoJSON = require('mongoose-geojson-schema');
let dogSchema = new mongoose.Schema({
//Some code omitted
location: {
type: mongoose.Schema.Types.Point,
coordinates: [Number],
},
//Some code omitted
});
dogSchema.index({"location": "2dsphere"});
module.exports = mongoose.model("Dog", dogSchema);
这种输入:
{
"created" : ISODate("2018-03-04T02:17:58.868Z"),
"name" : "Babee",
"breed" : "Puddle",
"age" : 11,
"owner" : ObjectId("5a9b5756a1e44812d109f191"),
"location" : { "type" : "Point", "coordinates" : [-118.4912, 34.0195] },
"__v" : 0
}
我执行此查询:
Dog.find({
$near: {
$geomentry: {
type: "Point",
coordinates: [-118.4452, 34.0689],
}
}
}, function (error, result){
//Some code omitted
});
使用此索引:
index: '2dsphere'
产生此错误:
{ MongoError: unknown top level operator: $near
at queryCallback (/Users/varun/Desktop/Programing/Woof/node_modules/mongodb-core/lib/cursor.js:223:25)
at /Users/varun/Desktop/Programing/Woof/node_modules/mongodb-core/lib/connection/pool.js:541:18
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
name: 'MongoError',
message: 'unknown top level operator: $near',
ok: 0,
errmsg: 'unknown top level operator: $near',
code: 2,
codeName: 'BadValue' }
如何解决此错误?
更新
看来我有一个错误,索引被删除 Link