updateOne() का उपयोग
updateOne update operators इस्तेमाल करते हुए एक filter match करने वाला पहला document बदलता है।
In this page:
Syntax
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()
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. #}
आम गलतियां
- operators की बजाय एक plain document pass करना
- यह मान लेना कि updateOne हर match update करता है
- 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: