findOneAndDelete()
Reference group: Collection CRUD methods
What it does
Atomically deletes and returns one matching document.
Syntax
db.collection.findOneAndDelete(filter, options)
Use case
Claim-and-remove a queue item when deletion is intended.
findOneAndDelete(): practical example
db.jobs.findOneAndDelete({ status: "queued" }, { sort: { priority: -1 } })💡 Explanation
Atomically deletes and returns one matching document.
When to use it
Claim-and-remove a queue item when deletion is intended.
Key Takeaways
- Atomically deletes and returns one matching document.
- Typical use: Claim-and-remove a queue item when deletion is intended.