//The results are as follows: I want to
{
"UserId" : "ace002",
"TargetUsers" : [
{"TargetUserId" : "bce005","MultipulUsers" : [ { "MultipulUserId" : "cce005" } ] }
]
}
How do I get the same results as above?
Hi,
I think you are looking for the new $elemMatch projection in MongoDB 2.2, which limits output to the first matching element of an array:
db.collections.find({ "UserId":"ace002"}, { _id: 0, UserId:1, "TargetUsers": { $elemMatch: {TargetUserId: "bce005"} }} )
Your original syntax was using $elemMatch in the query rather than the projection ..
Cheers,
Stephen