Can someone help me with a query on how to remove strings from an array in mongodb only if the string has a substring "abc.com"
Current result in a collection :
{
_id: '1',
users: ['[email protected]', '[email protected]', '[email protected]']
}
{
_id: '2',
users: ['[email protected]', '[email protected]', '[email protected]']
}
Expected Result :
{
_id: '1',
users: ['[email protected]']
}
{
_id: '2',
users: ['[email protected]', '[email protected]']
}
2
Answers
For a read-only query, you can use the
$filter
operator to filter the value with doesn’t match the domain address via$not
and$regexMatch
operator.Demo (Read-only) @ Mongo Playground
For updating the document(s), you need to use the
$pull
operator to remove the value from the array if matches the regex.Demo (Update) @ Mongo Playground
Maybe this work for you: