← Back to MongoDB Course | Chapter 2: CRUD — Create | Lesson 2 of 7

insertMany() का प्रयोग

insertMany एक call में कई documents add करता है, जो कई single inserts से तेज़ है।

In this page:

  1. insertMany()
Syntax
javascript
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()

bash
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. #}
आम गलतियां
  1. एक array की बजाय separate arguments pass करना
  2. जब partial success ठीक हो ordered false इस्तेमाल न करना
  3. एक 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:

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.