I am working on a Java system, part of which maps expressions from an internal representation to a MongoDb request. I develop the expressions "AND" in the same way as for the expressions "OR". Therefore, if I have an expression like "category =" GIBBLY "AND active = true", it will display as:
{$and:[{"category":"GIBBLY"},{"active":true}]}
I understand that in this case it is not necessary. I can’t understand if there is ever a case when $ is necessary (on my system there will often be nested AND and OR, if that matters).
The reason this is a problem for me is because $ and the operator seem to be failing for geospatial queries. The request is:
{"$and":[{"loc":{"$nearSphere":[-79.75 , 43.5], "$maxDistance":0.00156787}}, {"active":true}]}
fails with the error "cannot find special index: 2d". If I re-request as:
{"loc":{"$nearSphere":[-79.75 , 43.5], "$maxDistance":0.00156787}, "active":true}
It works great.
I tried to do some research and found a discussion of the problem in the mongodb user group , which talked about discussing composite indexes in geospatial queries .
None of the information found explains why exactly $ does not work with geospatial query. I have no composite indexes that are not defined at all in my database, and not $ and query work, so I'm not sure how $ works differently with respect to indexes.
To summarize, my questions are:
Is $ ever necessary, and if so, in what cases?
Does $ and in some fundamental way differ from the implicit representation with respect to indices?
$ ?