insertOne()

Inserts a single document into a collection. Returns an acknowledgment with the inserted document's _id.

Syntax

mongodb
db.collection.insertOne(document)

Example

mongodb
const result = await db.users.insertOne({
  name: "Alice",
  email: "alice@example.com",
  role: "user",
  createdAt: new Date()
});
console.log(result.insertedId); // ObjectId