As Sammaye has already pointed out, no more than one element of a positional array is currently indicated.
$elemMatch . $elemMatch , $elemMatch:
db.books.find(
{ $or: [{ "readers.name": "Johny" }, { "likes.name": "Johny" }] },
{
readers : { $elemMatch : { name : "Johny" }},
likes : { $elemMatch : { name : "Johny" }}
}
);
Edit
Altong MongoDB , , , , . , , :
db.books.aggregate([
{ $match: { $or: [{ "readers.name": "Johny" }, { "likes.name": "Johny" }]}},
{ $unwind: '$likes' },
{ $group : {
_id : '$_id',
likes : {
$push : {
$cond : [
{ $eq : ["$likes.name", "Johny"]},
"$likes",
null
]
}
},
readers : { $first : "$readers" },
name : { $first : "$name" }
}},
{ $unwind: '$readers' },
{ $group : {
_id : '$_id',
readers : {
$push : {
$cond : [
{ $eq : ["$readers.name", "Johny"]},
"$readers",
null
]
}
},
likes : { $first : "$likes" },
name : { $first : "$name" }
}},
{ $project : {
name : 1,
readers : { $setDifference : [ "$readers", [null] ] },
likes : { $setDifference : [ "$likes", [null] ] },
}}
]);
, "" $push $cond $push. null. , setDifference.
, / , , , .