I am facing always the same thing when I would like to query macting subdocuments of an array.
Lets think I have a collection like below:
{
"_id" : 1,
"value" : 5,
"array" :[ {"name" : "John", "born" : ISODate("1980-04-17T13:11:54Z"), "hasACar" : false},
{"name" : "Alice", "born" : ISODate("1985-04-17T13:11:54Z"), "hasACar" : false},
{"name" : "Jordan", "born" : ISODate("1980-05-19T104:11:54Z"), "hasACar" : true} ]
}
{
"_id" : 2,
"value" : 3,
"array" :[ {"name" : "Neil", "born" : ISODate("1985-11-14T16:11:54Z"), "hasACar" : false},
{"name" : "Marin", "born" : ISODate("1987-08-17T13:15:00Z"), "hasACar" : false}]
}
{
"_id" : 3,
"value" : 7,
"array" :[ {"name" : "Micheal", "born" : ISODate("1975-04-17T13:11:54Z"), "hasACar" : false},
{"name" : "Lisa", "born" : ISODate("1985-04-17T13:11:54Z"), "hasACar" : true},
{"name" : "Pascal", "born" : ISODate("1965-05-19T104:11:54Z"), "hasACar" : false} ]
}
{
"_id" : 4,
"value" : 1,
"array": [ {"name" : "Dave", "born" : ISODate("1980-04-17T13:11:54Z"), "hasACar" : true},
{"name" : "Paul", "born" : ISODate("1985-04-17T13:11:54Z"), "hasACar" : false},
{"name" :"Chuck", "born" : ISODate("1980-05-19T104:11:54Z"), "hasACar" : true} ]
}
I would like to retrieve all documents with only matching subdocuments. The criteria is born after 1979 and hasACar is false, which means omit people who has a var, and born before 1979, then return documents.
I could make this mathing with Aggregation framework but I am curious about is there another way to make it without using aggregationor mapreduce.
I tried to used $elemMatch operator in projection case when querying but it returns documents with only first matching subdocument.
Any help or advice appreciated.
Thx.