Typorithm · easy

Insert Documents

mongo-insertmongodbinsertdocuments_id
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Insert Documents

0%

CRUD

insertOne adds a single document; insertMany takes an array of documents. MongoDB auto-generates a unique _id for each unless you supply one.

A collection holds documents — JSON-like objects whose fields can be strings, numbers, arrays, or nested objects.

insertOne writes a single document and returns the new _id; insertMany takes an array and writes each element as its own document.

Every document gets a unique _id: an ObjectId by default, or whatever value you set explicitly on the _id field.

01

insertOne writes one document.

02

insertMany writes an array of documents.

03

_id is auto-generated unless supplied.

db.users.insertOne({ name: "Ada", age: 36 })
db.users.insertMany([
  { name: "Linus", age: 54 },
  { name: "Grace", age: 41 },
])

Read the implementation

Connect each part of the code to the concept before you type.

1. Insert one

insertOne adds a single document and returns its generated _id.

db.users.insertOne({ name: "Ada", age: 36 })

2. A document

A document is a JSON-like object; fields can be any BSON type.

{ name: "Ada", age: 36 }

3. Insert many

insertMany takes an array and inserts each element as its own document.

db.users.insertMany([

4. Array element

Each array element becomes one document; each gets its own _id.

{ name: "Linus", age: 54 },

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Insert Documents: Lesson & Practice · Typorithm | ApplyDjinn