gpt4 book ai didi

node.js - 使用 geoNear 时出现 node-mongodb-native 错误

转载 作者:可可西里 更新时间:2023-11-01 09:44:21 24 4
gpt4 key购买 nike

我到处都看过了,就是想不通……我可以让它在 mongo shell 中工作,但不能在我的应用程序中工作。这是代码。我可以让它在这里工作...(使用 MongoDB shell)

db.runCommand({geoNear: 'prints', near: {type:"Point",coordinates:[30.3,-40]},       spherical:true})

但它在我的应用程序中不起作用(使用 native 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 direclty 时有效)但是我无法让它与 native Node 驱动程序一起工作......我不断收到一个非常神秘的错误......

{
name: "MongoError"
errmsg: "exception: 'near' field must be point"
code: 17304
ok: 0
}

当我查找该错误时,它已被列出,但出于某种原因我得到了 404 页面???任何帮助将不胜感激,我已经用头撞墙一天了......

更新:我通过变通方法获得了这个...通过 native 驱动程序直接向 Mongo 强制命令...但我仍然对为什么 native 驱动程序不支持 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);}
});

向本地驱动程序团队提出功能请求?

最佳答案

我认为您在 MongoDB 的 native 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

关于node.js - 使用 geoNear 时出现 node-mongodb-native 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24664334/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com