insertMany() का प्रयोग
insertMany एक call में कई documents add करता है, जो कई single inserts से तेज़ है।
In this page:
Syntax
db.collection_name.insertMany([
{ field: value1 },
{ field: value2 }
], { ordered: false })
insertMany()
insertMany को documents का एक array pass करें। Default से inserts ordered हैं और पहले error पर रुक जाते हैं; errors से आगे जारी रखने के लिए { ordered: false } pass करें। Result position से inserted ids list करता है। Batches network round trips भी कम करते हैं।
Note:
ordered: false valid documents को through होने देता है भले ही एक fail हो।
उदाहरण: insertMany()
test> db.products.insertMany([
... { _id: 1, name: "Pen", price: 3, stock: 100 },
... { _id: 2, name: "Notebook", price: 8, stock: 40 },
... { _id: 3, name: "Lamp", price: 45, stock: 5 }
... ])
{ acknowledged: true, insertedIds: { '0': 1, '1': 2, '2': 3 } }
test> db.products.countDocuments()
3
⚠️ 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. #}
आम गलतियां
- एक array की बजाय separate arguments pass करना
- जब partial success ठीक हो ordered false इस्तेमाल न करना
- एक call में huge batches भेजना
चैप्टर सारांश
- insertMany एक array लेता है
- Ordered inserts पहले error पर रुकते हैं
- ordered: false जारी रहता है
- Batching round trips कम करता है
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: