我到处都找过了,就是想不通...我可以让它在mongo shell中工作,但不能在我的应用程序中工作。这是代码。我可以让它在这里工作。(使用MongoDB外壳)
db.runCommand({geoNear: 'prints', near: {type:"Point",coordinates:[30.3,-40]}, spherical:true})但它在我的应用程序中不起作用(使用原生mongodb驱动程序)
db.prints.geoNear({type: 'Point',coordinates:[30.3,-40]}, {spherical:true}, function(err, docs){
if(err){res.json(400, err);}
else{res.json(200, docs);}
});我不知道问题出在哪里?我非常确定我的索引是正确的(就像我使用Mongo Shell目录时一样),但我不能让它与本机节点驱动程序一起工作……我一直收到一个非常隐秘的错误...
{
name: "MongoError"
errmsg: "exception: 'near' field must be point"
code: 17304
ok: 0
}当我查找这个错误时,它被列出了,但由于某种原因,我得到了一个404页面?任何帮助都将不胜感激,我已经用头撞了这堵墙一天了……
更新:这是我通过一个变通方法得到的。通过本机驱动程序直接将命令强制发送到Mongo...但我仍然不明白为什么原生驱动程序不支持geoJSON --相反,geoNear命令强制使用传统坐标系……
mongo.db.command({
geoNear: 'prints', near: {"type":"Point","coordinates":[30.3,-40]}, spherical: true, num: 10000, maxDistance: req.body.radius
}, function(err, docs){
if(err){res.json(400, err);}
else{res.json(200, docs);}
});向本地驱动程序团队提出功能请求?
发布于 2014-07-10 09:10:28
我相信你在MongoDB的原生node.js驱动中调用geonear的语法是不正确的。使用您的示例,它应该是这样的:
db.prints.geoNear(30.3,-40, {spherical:true}, function(err, docs){
if(err){res.json(400, err);}
else{res.json(200, docs);}
});有关geonear()方法调用选项的更多详细信息,可以在文档中找到:
http://mongodb.github.io/node-mongodb-native/api-generated/collection.html
https://stackoverflow.com/questions/24664334
复制相似问题