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

Bulk में Insert करना

बड़े loads के लिए, batches में insert करें या कई operations साथ भेजने के लिए bulkWrite इस्तेमाल करें।

In this page:

  1. Bulk Insert
Syntax
javascript
db.collection_name.insertMany(documents, { ordered: false })
db.collection_name.bulkWrite([
  { insertOne: { document: doc } },
  { deleteOne: { filter: filter } }
])

Bulk Insert

ordered: false के साथ insertMany सबसे simple bulk insert है। bulkWrite एक round trip में inserts, updates, और deletes mix करता है और हर type के लिए counts report करता है। लाखों documents के लिए mongoimport या कुछ हज़ार के batches इस्तेमाल करें।

Note: 1,000 से 10,000 documents के batches एक अच्छा starting point हैं।

उदाहरण: Bulk insert

bash
test> const docs = [];
test> for (let i = 1; i <= 5; i++) docs.push({ _id: i, n: i, square: i * i });
test> db.squares.insertMany(docs)
{
  acknowledged: true,
  insertedIds: { '0': 1, '1': 2, '2': 3, '3': 4, '4': 5 }
}
test> db.squares.find({ square: { $gte: 9 } }, { _id: 0 })
[ { n: 3, square: 9 }, { n: 4, square: 16 }, { n: 5, square: 25 } ]
test> db.squares.countDocuments()
5

⚠️ 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. एक loop में हर document के लिए एक insert
  2. एक single enormous batch भेजना
  3. per-operation results ignore करना
चैप्टर सारांश
  • ordered false के साथ insertMany
  • bulkWrite operation types mix करता है
  • हज़ारों के Batch sizes
  • mongoimport files load करता है
🔒

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.