获取错误"mongodb unable to find index for $geoNear query"



我正在尝试将位置数据从我的安卓应用程序放入 mongo db 数据库。数据正在插入到数据库中。但是,在检索时,我收到一个错误,说:

" "$err" : "Unable to execute query: error processing query: ns=demo.search limit=0 skip=0nTree: GEONEAR  field=loc maxdist=1 isNearSphere=0nSort: {}nProj: {}n planner returned error: unable to find index for $geoNear query",
...         "code" : 17007
"

我已经使用 createIndex 命令将 loc index 添加为 2dsphere。我的查询到底出了什么问题?

我的查询:

db.collection('search').find(
{"loc":
{ $near :coords ,$maxDistance: maxDistance}
}
);

通过创建带有 2d 的索引来解决它。最初我尝试了 2dsphere,这就是我的代码不起作用的原因。

最新更新