← Back to MongoDB Course | Chapter 4: CRUD — Update | Lesson 1 of 7

updateOne() का उपयोग

updateOne update operators इस्तेमाल करते हुए एक filter match करने वाला पहला document बदलता है।

In this page:

  1. updateOne()
Syntax
javascript
db.collection_name.updateOne(
  filter,
  { $set: { field: new_value } }
)

updateOne()

db.collection.updateOne(filter, update) ज़्यादा से ज़्यादा एक document modify करता है। update document को $set जैसे operators इस्तेमाल करने ज़रूरी हैं, नहीं तो आपको एक error मिलता है।

Result matchedCount और modifiedCount report करता है, जो अलग होते हैं जब नई value old के बराबर हो।

Note: modifiedCount 0 है जब update कुछ नहीं बदलता।

उदाहरण: updateOne()

bash
test> db.users.insertMany([{ _id: 1, name: "Ada", age: 36 }, { _id: 2, name: "Bob", age: 25 }])
{ acknowledged: true, insertedIds: { '0': 1, '1': 2 } }
test> db.users.updateOne({ _id: 1 }, { $set: { age: 37 } })
{ acknowledged: true, matchedCount: 1, modifiedCount: 1 }
test> db.users.updateOne({ _id: 1 }, { $set: { age: 37 } })
{ acknowledged: true, matchedCount: 1, modifiedCount: 0 }
test> db.users.updateOne({ _id: 99 }, { $set: { age: 1 } })
{ acknowledged: true, matchedCount: 0, modifiedCount: 0 }
test> db.users.find()
[
  { _id: 1, name: 'Ada', age: 37 },
  { _id: 2, name: 'Bob', age: 25 }
]

⚠️ 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. operators की बजाय एक plain document pass करना
  2. यह मान लेना कि updateOne हर match update करता है
  3. matchedCount बनाम modifiedCount ignore करना
चैप्टर सारांश
  • updateOne पहला match बदलता है
  • $set जैसे operators इस्तेमाल करें
  • matchedCount बनाम modifiedCount
  • _id जैसा एक unique filter add करें
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 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.