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.
| Category | Admin Commands |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
db.orders.insertOne({ item: "apple", qty: 10 })
db.runCommand({
insert: "orders",
documents: [ { item: "apple", qty: 10 }, { item: "pear", qty: 5 } ]
})
⚙️ Parameters
| documents | Array of documents to insert |
|---|---|
| ordered | true 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
- Missing _id fields get auto-generated ObjectIds, echoed back
- Single documents cap at 16MB; use GridFS for larger files
- If any document fails, the whole command inserts nothing
🏷️ Related Tags
💡 Related Cases
📚 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.