I want to use aggregation to get this array only with those tickets, which have start field after 2015-06-16. Can someone help me with the pipeline?
{
"name" : "array",
"tickets" : [
{
"id" : 1,
"sort" : true,
"start" : ISODate("2015-06-15T22:00:00.000Z")
},
{
"id" : 2,
"sort" : true,
"start" : ISODate("2015-06-16T22:00:00.000Z")
},
{
"id" : 3,
"sort" : true,
"start" : ISODate("2015-06-17T22:00:00.000Z")
}
]
}
It's true that the "standard projection" operations available to MongoDB methods such as
.find()
will only return at most a "single matching element" from the array to that is queried by either the positional$
operator form in the "query" portion or the$elemMatch
in the "projection" portion.In order to do this sort of "ranged" operation, you need the aggregation framework which has greater "manipulation" and "filtering" capabilities on arrays:
Or you can possibly use the
$redact
operator to simplify with MongoDB 2.6 or greater which basically uses the$cond
operator syntax as it's input:So both examples do the "same thing" in "filtering" the elements from the array that "do not" match the conditions specified and return "more than one" element, which is something basic projection cannot do.