Getting empty list from JavaScript for geoNear query that works fine in Mongo shell

32 views
Skip to first unread message

Rick Rafey

unread,
Oct 28, 2014, 8:09:44 AM10/28/14
to mongoo...@googlegroups.com
I am trying to migrate my code from using $near to geoNear, as I want the sort order of the results to be based on distance.

My database is called 'testData' and my collection with locations is 'restaurants'.

When I run this command in the Mongo shell, I get exactly the results I want:

db.runCommand( {"geoNear":"restaurants","near":[-122.392247,37.792834],"spherical":true,"maxDistance":5000,"query":{},"num":10});

When I try to run the same command in my JavaScript routes.js file, as in:

var Restaurants = require('./models/Restaurants.js');
...
var maxentries = 10;
var lng = -122.392247;
var lat = 37.792834;
var point = {type: 'Point', coordinates: [lng, lat]};
Restaurants.geoNear(point, maxDistance:5000, spherical: true, num: maxentries})

I get an error that geoNear is not a method of Restaurants, which I don't understand as I indexed it (see Restaurants.js).

So I'm trying to invoke it via a command as in:

        var cmdfilter = {
         "geoNear": "restaurants",
          "near":[lng, lat],
          "spherical":true,
          "maxDistance":5000,
          "query":{},
          "num":10
        };

        Restaurants.db.db.command(cmdfilter, {}, function(err, restaurants) { ...

and I always get an empty list. I honestly don't understand the "db.db" syntax on the command method, which I got from an example, but all indications are that it's checking the object properly.

I'd appreciate any help on this as I'd really like to be able to sort my results by distance, and I've also heard that $near doesn't work on sharded collections when I scale up.
Restaurants.js
Reply all
Reply to author
Forward
0 new messages