Thuta Learning
IntermediateData & Databasesintermediate

Insert Documents

Relax. We'll talk through this in plain words — no textbook voice.

Use the insertOne() or insertMany() methods to add new documents into a collection.

javascript
// Insert a single document
db.inventory.insertOne(
   { item: "canvas", qty: 100, tags: ["cotton"], size: { h: 28, w: 35.5, uom: "cm" } }
)

// Insert multiple documents
db.inventory.insertMany([
   { item: "mat", qty: 85, tags: ["gray"] },
   { item: "mousepad", qty: 25, tags: ["gel", "blue"] }
])
You should see
{ "acknowledged": true, "insertedId": ObjectId("...") } ...