MongoDB geospatial query with $ not

I have a basic geospatial query that works well in MongoDB. It seems like it would be easy to apply $ so as not to get the add-on ... but this does not work for me. Simple user error? Or can MongoDB conceptually not handle this request? I could not find such a limitation in the documentation.

Work request (correctly finds 2 cities within a radius of 10 miles from the center):

db.customers.find({ "location" : { $within : { $center : [[-117.15,32.72],0.15] } } }) 

Attempt to request a supplement (the desired result is 1 city that is not within 10 miles):

 db.customers.find({ "location" : { $not : { $within : { $center : [[-117.15,32.72],0.15] } } } }) error: { "$err" : "missing geo field (location) in : {location: { $not: { $within: { $center: [ [ -117.15, 32.72 ], 0.15 ] } } } }", "code" : 13042 } 

For those who want to copy / paste a query to see the error, here is a tiny bit of sample data:

 db.customers.ensureIndex( { "location" : "2d" } ) db.customers.save({"city":"La Mesa","state":"CA","location":[ -117.02,32.76 ]}) db.customers.save({"city":"Chula Vista","state":"CA","location":[ -117.08,32.63 ]}) db.customers.save({"city":"Mexico City","state":"Mexico","location":[-99.133244,19.4326]}) 

(I use MongoDB 1.8.2 in case that matters.)

+7
source share
1 answer

I think that this is impossible. As far as I know, location requests will provide you with a special cursor that can only use location requests as parameters (e.g. $within ).

v. 2.0.1 gives a more descriptive error message: error: { "$err" : "geo field only has 1 element", "code" : 13068 }

The problem with indexing is that, generally speaking, negation is EVIL. Most indexes do not do very well when you rotate them, so even if your query worked, this is probably undesirable because you probably have to scan the table.

I'm not quite sure about this; a newsgroup post is probably a good idea.

+5
source

All Articles