MongoDB : use $ positional operator for querying MongoDB : use $ positional operator for querying mongodb mongodb

MongoDB : use $ positional operator for querying


Yes, you are correct - the positional operator is used for updating an object.

The solution for now would be to return the array an pull the field out in your application.

There is an open enhancement request for this feature (in queries):

https://jira.mongodb.org/browse/SERVER-828

For more information on the positional operator, see:

http://www.mongodb.org/display/DOCS/Updating#Updating-The%24positionaloperator


What you are looking for is the $elemMatch operator.


It might be an overkill, but I guess you can use map-reduce for this.

first, pre-filter with a query, emit all array elements in map, filter the ones that do not match either in emit or in reduce. If you don't set out everything will happen in RAM.

If you have to run those kinds of queries often, it might be worthwhile to duplicate the data.

Nevertheless, I hope the SERVER-828 will be implemented soon!