Instead of a $ positional operator in projections ( as proposed here: http://jira.mongodb.org/browse/SERVER-828 ), I think that we could use a $elemMatch operator in projections, which will be used similiar to $slice.
An example in pseudo-js:
doc = {'a' : [
,
{'b':2},
{'b':3}],'c' :4}
project( {'a' : [
{'b':1},
{'b':2},
{'b':3}] }, {'a':{$elemMatch:
{'b':2}}} )
will be evaluated to:
{'a' : [
],'c' :4}
project( {'a' : [
{'b':1},
{'b':2},
{'b':3} ] }, {'a':{$elemMatch:{'b':{'$gte':2}}}} )
will be evaluated to:
{'a' : [
,
{'b':3}],'c' :4}
The advantage over the $-operator is that, the projection is completly independent from the matching. So you could match on something completly different and return only the entries you want. Furthermore this should be easier to implement.
The only problem I see is that, you will in many cases repeat yourself, because you want to extract the fields you are querying for.
- is related to
-
SERVER-828 Support for selecting array elements in return specifier (projection)
- Closed
- related to
-
SERVER-6613 using $elemMatch projection with $all gives unexpected results
- Closed
-
SERVER-6612 Support projecting multiple array values in a projection like the $elemMatch projection specifier
- Closed
-
CSHARP-683 Add new features to Query builder for server 2.4
- Closed