Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node-mongodb-native error when using geoNear

I've looked everywhere, and simply can't figure this out... I can get it to work in the mongo shell, but not in my application. Here's the code. I can get it to work here... (using the MongoDB shell)

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

but it does not work in my app (using the native mongodb driver)

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);}
});

I have no idea what the issue is? I'm pretty sure my index is correct (as it works when I use the Mongo Shell direclty) but I can't get it to work with the native node driver... I keep getting a really cryptic error back...

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

and when I look that error up, it's listed, but I get a 404 page for some reason??? Any help would be greatly appreciated, I've been banging my head against this wall for a day...

UPDATE: I got this via a work-around... by forcing a command through the native-driver directly to Mongo... but I'm still perplexed as to why the native-driver doesn't support geoJSON - instead the geoNear command is forcing a legacy coordinate system...

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);}
});

Feature request to the native-driver team?

like image 927
joshula Avatar asked Sep 01 '26 07:09

joshula


1 Answers

I believe your syntax for calling geonear in the native node.js driver for MongoDB is incorrect. Using your example, it should be something like this:

db.prints.geoNear(30.3,-40, {spherical:true}, function(err, docs){
    if(err){res.json(400, err);}
    else{res.json(200, docs);}
});

More detail on calling options for the geonear() method can be found here in the documentation:

http://mongodb.github.io/node-mongodb-native/api-generated/collection.html

like image 196
John Petrone Avatar answered Sep 02 '26 22:09

John Petrone