Reputation: 25
For a mongodb collection with 30+ million documents is it better to:
1: Query multiple times like this:
for (const _id of array) {
await Schema.deleteOne({ _id: _id })
}
2: One query using $in
:
await Schema.deleteMany({ _id: { $in: array } })
Upvotes: 1
Views: 864
Reputation: 15207
Since a for loop needs multiple conections to DataBase is a better way use deleteMany
.
Into Mongo docs from deleteOne
it says:
To delete multiple documents, see db.collection.deleteMany()
Also, for small databases, a for
loop may have a not bad performance but with large amount of data this is not a good idea because you are calling your DB multiple times. And also using await
it stops the execution so this is not efficient.
Upvotes: 1