I have the following document
db.c.save({a:[{u:3},{u:6},{u:123}]});
I want to fetch matching elements from the array. So I use the following q
Unfortunately, the $ positional operator only returns the first match so you can't use it to do what you are trying to do.
However, you can use either aggregation or map-reduce. The following code does what you want using the aggregation framework :
db.c.aggregate([
{ $unwind : "$a"},
{ $match : { "a.u" : {$in :[3,123]} } },
{ $group : {_id : "$_id",a : { $push : "$a" } } }
])