Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongodb geoNear + additional filters?

Is it possible to add more filters when using geoNear in mongodb? For example, say I my records look like the following:

{
 _id: {},
 cid: 1,
 latlon: [ -74.07096147537231, 40.9088747684256 ]
}

Can I pass "cid" to be sure that only the records with a "cid" that equals "1"? If it is not possible with geoNear, how would I do this? I am using geoNear because it returns the distance...

Thanks!

like image 844
mike Avatar asked Dec 22 '22 15:12

mike


1 Answers

Yes, sure it's possible. You can use any filtering with $near as usual:

db.places.find( { latlon: { $near : [50,50] } }, {cid: 1} )

Update:

If you need distance use db.runCommand, if no need distance -- db.collection.find as usual.

From documentation:

The geoNear command has the added benefit of returning the distance of each item from the specified point in the results, as well as some diagnostics for troubleshooting.

There is query parameter in db.runCommand, you can use it like this:

db.runCommand( { geoNear : "latlon" , near : [ 50 , 50 ], num : 10,
                 query : { cid: 1 } } );
like image 88
Andrew Orsich Avatar answered Dec 28 '22 05:12

Andrew Orsich