query = {
"location" : {
"$near" : {
"$geometry": {
"type": "Point" ,
"coordinates": [18.55,73.78]
}
}
}
}
}
planner returned error: unable to find index for $geoNear query
at Connection.<anonymous> (D:\Projects\api\node_modules\mongodb-core\lib\connection\pool.js:443:61)
at Connection.emit (events.js:198:13)
at Connection.EventEmitter.emit (domain.js:466:23)
at processMessage (D:\Projects\api\node_modules\mongodb-core\lib\connection\connection.js:364:10)
at Socket.<anonymous> (D:\Projects\api\node_modules\mongodb-core\lib\connection\connection.js:533:15)
at Socket.emit (events.js:198:13)
at Socket.EventEmitter.emit (domain.js:466:23)
at addChunk (_stream_readable.js:288:12)
at readableAddChunk (_stream_readable.js:269:11)
at Socket.Readable.push (_stream_readable.js:224:10)
at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
ok: 0,
为此,我创建了 2dsphere 在以下链接中指定的位置字段上
https:/docs.mongodb.commanualcore2dsphere。
db.collectionName.createIndex( { location : "2dsphere" } )
帮助我了解我是否做错了什么。
尝试改变 $near
到 $nearSphere
并加 $minDistance: 0
和 $maxDistance: yourmaxdistance
旁边 $geometry
如果还是不行的话......那么它就会看起来像
query = {
location: {
$nearSphere: {
$geometry: {
type: "Point" ,
coordinates: [18.55,73.78],
},
$minDistance: 0,
$maxDistance: //your max will go here
}
}
}
如果上面的方法不奏效,请再次尝试创建索引,并留意输出结果......。
下面是一个创建索引失败的例子......
{
"ok" : 0,
"errmsg" : "Unknown index plugin '2dSphere'",
"code" : 67,
"codeName" : "CannotCreateIndex",
"operationTime" : Timestamp(1590940517, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1590940517, 1),
"signature" : {
"hash" : BinData(0,""),
"keyId" : NumberLong("")
}
}
}