mike
mike

Reputation: 8141

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!

Upvotes: 4

Views: 4212

Answers (1)

Andrew Orsich
Andrew Orsich

Reputation: 53675

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

Upvotes: 11

Related Questions