Reputation: 598
I am trying to remove a specific array item from multiple documents. Let's say we have the following documents:
{
_id: ...,
email: '[email protected]',
tasks: [
{ title: 'some titleA', uuid: 'uuidA'},
{ title: 'some titleB', uuid: 'uuidB'},
{ title: 'some titleC', uuid: 'uuidC'},
]
},
{
_id: ...,
email: '[email protected]',
tasks: [
{ title: 'some titleA', uuid: 'uuidA'},
{ title: 'some titleB', uuid: 'uuidB'},
{ title: 'some titleC', uuid: 'uuidC'},
]
},
{
_id: ...,
email: '[email protected]',
tasks: [
{ title: 'some titleA', uuid: 'uuidA'},
{ title: 'some titleB', uuid: 'uuidB'},
{ title: 'some titleC', uuid: 'uuidC'},
]
}
then, given a list of emails, say [email protected]
and [email protected]
remove from the tasks
array of any documents matching those emails the array item where uuid
value is uuidB
.
the result should be
{
_id: ...,
email: '[email protected]',
tasks: [
{ title: 'some titleA', uuid: 'uuidA'},
{ title: 'some titleC', uuid: 'uuidC'},
]
},
{
_id: ...,
email: '[email protected]',
tasks: [
{ title: 'some titleA', uuid: 'uuidA'},
{ title: 'some titleC', uuid: 'uuidC'},
]
},
{
_id: ...,
email: '[email protected]',
tasks: [
{ title: 'some titleA', uuid: 'uuidA'},
{ title: 'some titleB', uuid: 'uuidB'},
{ title: 'some titleC', uuid: 'uuidC'},
]
}
this is what I have tried so far:
var emails = ['[email protected]', '[email protected]']
model.updateMany({
email: { $in: emails },
}, {
$pull: {
tasks: {
$elemMatch: { uuid: uuid }
}
}
})
Thank you for your help!
Upvotes: 1
Views: 53
Reputation: 46451
$elemMatch
is an query
and projection
operator and not an update operator
. Therefore you cannot use it here.
You need to use "absolute" object structures
model.updateMany(
{ "email": { "$in": emails }},
{ "$pull": { "tasks": { "uuid": uuid }}}
)
Upvotes: 1