I have an array:
[
{
id: 1,
name: 1,
list: [
{
id: 11,
name: 11,
list: [
[
{ id: 111, name: 111 },
{ id: 112, name: 112 }
]
]
}
]
},
{
id: 6,
name: 6,
list: [
{
id: 62,
name: 12,
list: [ [ { id: 111, name: 111 } ] ]
}
]
}
]
I hope filter the second-level list array,use command
{
$project: {
id: 1, name: 1,
list: {
id: 1, name: 1,
list: {
$filter: {
input: '$list.list',
as: 'item',
cond: { $eq: ['$$item.name', 111] }
}
}
}
}
}
but not get the expected result. The complete filter code is as follows:
db.runoob.aggregate([{ $match: { $or: [{ 'name': 1, 'list.name': { $eq: 11 } }, { name: 6, 'list.name': { $eq: 12 } }] } }, { $project: { id:1, name: 1, 'list': { $filter: { input: '$list', as: 'item', cond: { $or: [{ $and: [{ $in: ['$$item.id', [11, 12]] }, {$eq: ['$$item.name', 11]}] }, { $and: [{ $in: ['$$item.id', [61, 62]] }, {$eq: ['$$item.name', 12]}] }] } } } } }, { $project: { id: 1, name: 1, list: { id: 1, name: 1, list: { $filter: { input: '$list.list', as: 'item1', cond: { $eq: ['$$item1.name', 111] } } } } } }])
Please help me,thanks ![]()