The following data structure:
Join me and have a fruit’s Document, which reads:
{
name: 'apple',
menu: [
{
Title:' with pears'
},
{
Title:' with strawberries'
}
]
},
{
name: 'orange',
menu: [
{
Title:' with pears'
},
{
Title:' with strawberries'
}
]
}
I want to searchPearWhen, get
{
name: 'apple',
menu: [
{
Title:' with pears'
}
]
},
{
name: 'orange',
menu: [
{
Title:' with pears'
}
]
}
In other words, how to clean the sub-arrays of the two-dimensional array?
If title doesn’t repeat, simply do the mapping
Db.test.find ({menu.title ":'with pears' }, {name: 1, "menu.$": 1})
But $ will only give you the first matching array element, so if title is duplicated, you need to use thefilterOperator. For specific usage, refer to the examples in the document.