我正在执行此查询
{
"$geoNear":{
"uniqueDocs":true,
"includeLocs":true,
"near":[
8.759131,
40.272393
],
"spherical":false,
"distanceField":"d",
"maxDistance":0.09692224622030236,
"query":{
},
"num":3
}
}
在此模型上:
var ridePathSchema = new Schema({
...
loc: [Number],
...
});
ridePathSchema.index({
loc: "2d"
});
我得到:
Unhandled rejection reason: MongoError:
can't find any special indices: 2d (needs index), 2dsphere (needs index)
有趣的是,就在这个查询之前,我确实对同一模型执行了类似的查询,但我对其进行了聚合并且它有效。
我做错了什么?
更新:
RidePaths.aggregate([query]) WORKS
RidePaths.find(query) CAN'T FIND INDEX
最佳答案
$geoNear 应作为 command 执行或者在聚合查询的第一阶段(正如您所注意到的)。
命令无法传递给 mongooses find() 方法。发出命令的唯一方法是通过驱动程序接口(interface)。有关如何执行此操作的工作示例,请参阅此要点:https://gist.github.com/aheckmann/5871847
请记住,驱动程序接口(interface)的结果不会是 mongoose 文档,因为 mongoose 被绕过。
关于mongodb - GeoNear Mongoose 和 2d 索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16747852/