MongoDB

Stage 2 · Lesson 1 of 9

Inserting documents

Reading and writing documents

Add documents with insertOne for one and insertMany for an of them. If a document has no _id, MongoDB adds an and returns it in the result as insertedId or insertedIds, so you never need a second query to find the new id.

A collection is created automatically on its first . insertMany is ordered by default: it inserts in sequence and stops at the first error, such as a duplicate _id, leaving the earlier documents inserted. Pass { ordered: false } to keep going past errors and get a list of the failures. are much faster than inserting documents one by one in a loop.

Common trapa typo in a collection name: db.prodcts.insertOne(...) silently creates a new collection instead of failing.

A question to check yourself, an AI practice session and 1 cheat sheet entry come with the full roadmap. Unlock

Resources for this stage

Start with

  • CourseFree
    MongoDB University

    MongoDB

    Free: the CRUD lessons on inserting, finding, replacing, updating and deleting documents, with labs

Practise

  • Test filters and updates on your own sample documents and share the link when you get stuck

Keep handy

Mark this stage as not done?