Using MongoDB $pull to delete documents within an Array
Solution 1:
That's exactly what the $pull
operator does, so in the shell you could use an update
like:
db.clusters.update({},
{$pull: {members: {tweetID: '5327010328645530500'}}},
{multi: true})
Set the multi
option so that every document is updated, not just the first one.
Solution 2:
This will delete multiple tweets
in a single query just pass an array to $in
:-
db.clusters.update({},
{$pull: {members: {$in: [ {tweetID: '5327010328645530500'},{"tweetID" : "2820402625046999289"} ] } } },
{multi: true});
The above method doesnt work in mongoose so for mongoose do:-
db.clusters.update({},
{$pull: {members:[ {tweetID: '5327010328645530500'},{"tweetID" : "2820402625046999289"} ] } });
Solution 3:
In the meantime the update method is deprecated. Therefore one possible up-to-date solution to this would be using the updateMany method instead.
db.clusters.updateMany({},
{$pull: {members: {tweetID: '5327010328645530500'}}})