Geo Query using $and

164 views
Skip to first unread message

rod

unread,
Nov 7, 2011, 2:19:05 PM11/7/11
to mongodb-user
Hi, I'm trying to use $and with a geo query, and getting an error. If
I run my query without the $and all is fine...

db.stuff.find({ coords: { '$near': [-10.17,30.47], '$maxDistance':
0.05 } });

i've a 2d index on coords obviously. but if I try to use $and (as i
want to also put other stuff in the query) i get an error...

db.stuff.find({ $and: [{ coords: { '$near': [-10.17,30.47],
'$maxDistance': 0.05 } }] })
error: {
"$err" : "can't find special index: 2d for: { $and: [ { coords:
{ $near: [ -10.17, 30.47 ], $maxDistance: 0.05 } } ] }",
"code" : 13038

the reason i am trying to use the $and is because i'd like to do
something like this:

db.stuff.find({ $and: [
{ geo part },
{ matches some tags },
// etc...
]});

I defined my index like this:

db.stuff.ensureIndex({ coords: "2d" });

So my questions are...

1) Do I need to define the index in a special way to use it in an
and? or...
2) Am I doing this the wrong way, is there a better way?

Thanks very much!

rod.

Bernie Hackett

unread,
Nov 7, 2011, 5:12:39 PM11/7/11
to mongodb-user
You don't need $and to do what you want to do. See here:

http://www.mongodb.org/display/DOCS/Geospatial+Indexing#GeospatialIndexing-CompoundIndexes

rod

unread,
Nov 8, 2011, 4:02:19 AM11/8/11
to mongod...@googlegroups.com
Aha!  That's great - thanks very much Bernie!

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.


Ayman Natsheh

unread,
Nov 14, 2011, 9:05:38 AM11/14/11
to mongod...@googlegroups.com
Hi,

I am facing the same problem, but I need to use the $and. 
How did you solve it rod ?
Is there a special way to define the index when using the $and ??? 

Thank you.

Bernie Hackett

unread,
Nov 14, 2011, 9:53:38 AM11/14/11
to mongodb-user
What are you trying to do that requires $and?

mdahlman

unread,
Nov 14, 2011, 6:39:26 PM11/14/11
to mongod...@googlegroups.com
I have a problem that I think is identical or closely analogous. In my case I want to use "$not" rather than "$and".
Is it simple user error? Or can MongoDB not handle this query conceptually?

I have this working query (correctly finds the 2 cities within 10 miles of the center):
db.customers.find({ "location" : { $within : { $center : [[-117.15,32.72],0.15] }

Attempted complement query (desired result is the 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 anyone that wants to copy/paste the query to see the error, here's 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'm using MongoDB 1.8.2 in case that matters.)

If this is threadjacking, just let me know. But it seems like it might be the same issue.

Regards,
Matthew Dahlman
Jaspersoft

Bernie Hackett

unread,
Nov 16, 2011, 4:34:47 PM11/16/11
to mongodb-user
MongoDB doesn't support this. I'm not even sure it makes much sense.
What your query says is "Give me every location stored in the
collection that isn't in this radius." That's fine if you only have a
few locations, but what happens when you have millions of locations?

mdahlman

unread,
Nov 16, 2011, 6:17:02 PM11/16/11
to mongodb-user
Agreed: "Show me all restaurants that are NOT within 5 miles of here"
is vastly less useful than showing me the ones that are within 5
miles.

On the other hand, it's not too hard to imagine more reasonable
variations. For example, maybe a data quality check:
Find locations in zipcode 12345 that are not within 100 miles of the
center of zipcode 12345. The expected result is 0 documents, but any
actual results get flagged as bad data.

Or maybe a "Rockin' the Suburbs" query that looks for clubs within 50
miles but not inside a polygon defining a city's downtown area.

Anyway, it was for a demo/proof of concept. I'm happy to drop the
issue. There are plenty of higher priority enhancements to add.

Thanks for the confirmation.

-Matt
Reply all
Reply to author
Forward
0 new messages