After searching for a whole day, I am doubting whether MongoDB can fulfill below requirement:
Q: How can I filter out documents that meet below conditions ?
- In last array element of
students_replies, there is a reply from a student whose name containing string 'ason'.
id_1: first_school, students_replies: [
{Date:20210101, replies: [
{name: jack, reply: 'I do not like this idea'},
{name: jason, reply: 'I would rather stay at home'},
{name: charles, reply: 'I have an plan to improve'},
]},
{Date:20210401, replies: [
...]},
{Date:20210801, replies: [
...]},
]
id_2: second_shool, students_replies: [..]
id_3: third_shool, students_replies: [...]
$in,$elemMatch,$indexOfBytein bothqueryandaggregation, none of them worked for me. Most of them will match the whole value instead of a portion (string matching to be specific) of that. for example,{key: 'This is apple'}, my matching condition will be containingplein the value, not value equals to'This is apple'. below is the valid json and expected results will be only documents withkey:1andkey:3will be outputted. mongoplayground.net/p/_-MFlpzF6eY