insert (Insert Documents)

Last updated: 2026-10-09

insert (Insert Documents)

insert is the low-level insertion command, normally wrapped by db.collection.insertOne()/insertMany() in mongosh. The raw form is db.runCommand({ insert: 'orders', documents: [...] }). It inserts many documents at once and auto-generates ObjectIds when _id is absent. Unlike insertMany's ordered mode, if any document in the insert command fails, none are inserted.

CategoryAdmin Commands
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

db.orders.insertOne({ item: "apple", qty: 10 })
db.runCommand({
  insert: "orders",
  documents: [ { item: "apple", qty: 10 }, { item: "pear", qty: 5 } ]
})

⚙️ Parameters

documentsArray of documents to insert
orderedtrue stops at the first error (default); false skips and continues

Returns:Returns n (inserted count), ok, and the inserted _ids

▶ Example

Run the commands below in your local mongosh (requires a running mongod):

// insert: batch insert via command
use shop
db.runCommand({
  insert: 'orders',
  documents: [
    { item: 'apple', qty: 10 },
    { item: 'pear', qty: 5 }
  ]
})

Output:

{ n: 2, ok: 1 }

📌 Notes

📚 Related Tutorials

❓ FAQ

QWhat differs between insert and insertMany?
AinsertMany allows partial success with ordered: false; the insert command inserts nothing if any document fails.
QHow do I get the inserted _ids?
AThe insert command echoes documents with _id; helpers return insertedIds.
QHow large can a single document be?
A16MB — the BSON document size limit.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏