3

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?

joshula
  • 535
  • 1
  • 5
  • 19
  • What are your versions of MongoDB server and Node.js driver? Also, what is the documentation url which leads to a 404? – Stennie Jul 21 '14 at 03:51

1 Answers1

2

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

John Petrone
  • 26,943
  • 6
  • 63
  • 68
  • Hi John, thanks for taking the time. I tried that, and it's treating my data as legacy coordinates vs. geoJSON (even though my data is geoJSON). That has implications in a few ways, most importantly, the distance is now specified in Radians (or something) vs. meters. I got the native-driver to run this command using geoJSON by speaking directly to Mongo... I'll post that code above. However, I'm really perplexed as to why the native-driver doesn't handle geoJSON? More info here [link](http://docs.mongodb.org/manual/core/2dsphere/) – joshula Jul 10 '14 at 15:37
  • you can convert radians to meters using the formula specified here: http://docs.mongodb.org/manual/tutorial/calculate-distances-using-spherical-geometry-with-2d-geospatial-indexes/ – John Petrone Jul 10 '14 at 15:47