I have an array in a sub, similar to this
{ "_id" : ObjectId("512e28984815cbfcb21646a7"), "list" : [ { "a" : 1 }, { "a" : 2 }, { "a" : 3 }, { "a" : 4 }, { "a" : 5 } ] }
Can I filter the subdocument for> 3
My expected result below
{ "_id" : ObjectId("512e28984815cbfcb21646a7"), "list" : [ { "a" : 4 }, { "a" : 5 } ] }
I try to use $elemMatch but returns the first matching element in the array
My request:
db.test.find( { _id" : ObjectId("512e28984815cbfcb21646a7") }, { list: { $elemMatch: { a: { $gt:3 } } } } )
The result returns one element in the array.
{ "_id" : ObjectId("512e28984815cbfcb21646a7"), "list" : [ { "a" : 4 } ] }
and I'm trying to use an aggregate with $match but not work
db.test.aggregate({$match:{_id:ObjectId("512e28984815cbfcb21646a7"), 'list.a':{$gte:5} }})
It returns the whole element in the array
{ "_id" : ObjectId("512e28984815cbfcb21646a7"), "list" : [ { "a" : 1 }, { "a" : 2 }, { "a" : 3 }, { "a" : 4 }, { "a" : 5 } ] }
Can I filter an element in an array to get the result as the result of the result?
filter mongodb mongodb-query aggregation-framework
chenka Feb 27 '13 at 16:21 2013-02-27 16:21
source share