CRUD Operations in MongoDB

CRUD means:

These are the basic database operations.

Create

await db.collection('users').insertOne({
  name: 'Asha',
  email: 'asha@example.com',
})

Create many:

await db.collection('users').insertMany([
  { name: 'Asha' },
  { name: 'Ravi' },
])

Read

Find one:

await db.collection('users').findOne({ email: 'asha@example.com' })

Find many:

await db.collection('users').find({ active: true }).toArray()

Update

await db.collection('users').updateOne(
  { email: 'asha@example.com' },
  { $set: { active: true } },
)

Use update operators like $set. Do not replace documents accidentally.

Delete

await db.collection('users').deleteOne({
  email: 'asha@example.com',
})

Interview answer

MongoDB CRUD operations are insert, find, update, and delete. insertOne creates documents, find and findOne read documents, updateOne modifies matching documents using update operators like $set, and deleteOne removes matching documents.