Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Collections & Methods  ›  Lesson

Inserting Documents

insert() and relevant 12 mininsertOne adds a single document and insertMany adds a list, each returning the new _id values, and an ordered insertMany stops at the first error while an unordered one carries on.

Inserting Documents

A supplier sends a list of new books. You could loop and insert them one by one, but that means a round trip per book and messy error handling. mongosh gives you two methods, one for a single document and one for a batch, and knowing how the batch behaves on errors saves real headaches.

The rule

insertOne for one, insertMany for a list; ordered batches stop at the first error.

db.newArrivals.insertOne({ title: "Kindred", author: "Octavia E. Butler", year: 1979, price: 6.75, stock: 4 })
// { acknowledged: true, insertedId: ObjectId("...") }

db.newArrivals.insertMany([
  { title: "Beloved", author: "Toni Morrison", year: 1987 },
  { title: "Matilda", author: "Roald Dahl", year: 1988 }
])
// { acknowledged: true, insertedIds: { '0': ObjectId("..."), '1': ObjectId("...") } }
  • The result tells you the new _id values. Documents without an _id get an ObjectId.
  • A document whose _id already exists fails with an E11000 duplicate key error.
  • insertMany is ordered by default: it inserts in order and stops at the first failure. Pass { ordered: false } to try every document and report all failures at the end.
  • Successful inserts are never rolled back by a later failure in the batch.

The old insert() method still appears in tutorials, but it is deprecated in mongosh; use the two above.

The shortcut

For a batch with a bad document, find the first failure, then ask: ordered or not? Ordered keeps everything before it. Unordered keeps everything except it.

Four books, the second is a duplicateStep 1 of 4
  1. Spot the failure

    Books 1 and 2 both carry _id: 101. Book 1 claims it; book 2 will fail with E11000.

The trap: re-running the whole batch after a failure. The books that succeeded now clash with themselves and fail as duplicates.

Drills

Answer fast — trust the rule.

quick drill

Which method adds a single document?

quick drill

An ordered insertMany of 5 documents fails on the 3rd. How many are inserted?

quick drill

The same batch runs with { ordered: false }. How many are inserted?

quick drill

What error does inserting an existing _id raise?

Remember

  • insertOne for one, insertMany for a list; ordered batches stop at the first error.
  • Shortcut: find the first failure; ordered keeps what came before, unordered keeps all but the failures.
  • Trap: nothing is rolled back, so never blindly re-run a batch that half succeeded.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainA batch with a duplicate

You call insertMany with four new books, but the second one reuses an _id that already exists. Explain what happens with the default ordered insert, what changes with { ordered: false }, and how you would know which documents made it in.