How to execute runCommand with Mongoose?

2,264 views
Skip to first unread message

Jose Simoes

unread,
May 6, 2011, 5:51:56 AM5/6/11
to Mongoose Node.JS ORM
I am using Node.js and Mongoose to access my MongoDB. I am using a
model that stores some geo coordinates. I have them indexed and
everything seems to work as expected. What I am trying to do is to
retrieve the most near things from my request. On the MongoDB console
I do something like this:

distances = db.runCommand({ geoNear : "deals", near : [11.252,
14.141], spherical : true, maxDistance : 300 }).results

However, I am not sure how to do this with Mongoose. Here is more
information about the command I am trying to use:
http://www.mongodb.org/display/DOCS/Geospatial+Indexing

Thanks, Jose

Aaron Heckmann

unread,
May 10, 2011, 9:35:19 PM5/10/11
to mongoo...@googlegroups.com, node-mong...@googlegroups.com
Right now we don't really support this directly and I'm not sure if the node-mongodb-native driver supports it either.

/cc node-mongodb-native mailing list
--
Aaron


christkv

unread,
May 11, 2011, 7:30:01 PM5/11/11
to Mongoose Node.JS ORM
Hi

Yes you can. On the db connection there is a function called

executeDbCommand

that let's you execute commands agains the db. So in this case it
would be something like this

db.executeDbCommand({ geoNear : "deals", near : [11.252,14.141],
spherical : true, maxDistance : 300 }, function(err, result {
// do something with results
});

Cheers

On May 11, 3:35 am, Aaron Heckmann <aaron.heckm...@gmail.com> wrote:
> Right now we don't really support this directly and I'm not sure if the
> node-mongodb-native driver supports it either.
>
> /cc node-mongodb-native mailing list
>

Joel Haasnoot

unread,
May 16, 2011, 4:24:11 AM5/16/11
to mongoo...@googlegroups.com
By the looks of it, I'm working in a similar space, but an alternative
to using the 'executeCommand' syntax is to use the normal find() with
the $within operator. My query looks like this, with point being your
location, and range being in km

locations.find({ 'loc' : { $within : { $centerSphere: [ point,
(range/6713) ] } }};

This is a little "cleaner", but has the disadvantage of not
calculating the distance between points, or providing the added
diagnostic info, both not information I'm interested in however.

Joel Haasnoot

Reply all
Reply to author
Forward
0 new messages