← Back to MongoDB Course | Chapter 5: CRUD — Delete | Lesson 3 of 6

findOneAndDelete() का उपयोग

findOneAndDelete एक document remove करता है और इसे return करता है, एक atomic step में।

In this page:

  1. findOneAndDelete()
Syntax
javascript
db.collection_name.findOneAndDelete(filter)

findOneAndDelete()

यह deleted document (या null) return करता है, जो job queues के लिए perfect है जहाँ एक worker अगला item claim करता है। कौन-सा matching document लिया जाए control करने के लिए sort इस्तेमाल करें और returned fields limit करने के लिए projection।

Note: एक queue से सबसे पुराना item pop करने के लिए sort से combine करें।

उदाहरण: findOneAndDelete()

bash
test> db.jobs.insertMany([{ _id: 1, task: "email", prio: 2 }, { _id: 2, task: "resize", prio: 1 }, { _id: 3, task: "backup", prio: 3 }])
{ acknowledged: true, insertedIds: { '0': 1, '1': 2, '2': 3 } }
test> db.jobs.findOneAndDelete({ prio: { $gte: 1 } })
{ _id: 1, task: 'email', prio: 2 }
test> db.jobs.findOneAndDelete({ task: "missing" })
null
test> db.jobs.countDocuments()
2

⚠️ Run this in your own terminal or Node.js environment.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. find फिर deleteOne इस्तेमाल करना, एक race बनाते हुए
  2. queue order के लिए sort भूल जाना
  3. एक null result ignore करना
चैप्टर सारांश
  • Document delete और return करता है
  • हर document के लिए Atomic है
  • sort चुनता है कौन-सा
  • कुछ match न होने पर null return करता है
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.