insertOne()

Reference type:Cheat sheetFormat:Syntax + example + use case

Reference group: Collection CRUD methods

What it does

Inserts one document and returns its inserted identifier.

Syntax

db.collection.insertOne(document)

Use case

Create a record and retain insertedId.

insertOne(): practical example
db.products.insertOne({ sku: "KB-1", price: 1999 })
💡 Explanation Inserts one document and returns its inserted identifier. When to use it Create a record and retain insertedId.
Key Takeaways
  • Inserts one document and returns its inserted identifier.
  • Typical use: Create a record and retain insertedId.