DEV Community

Abhay Singh Kathayat
Abhay Singh Kathayat

Posted on

Essential MongoDB Operations: A Guide to CRUD and Administration

MongoDB Operations


1. How do you create a database in MongoDB?

In MongoDB, you don’t explicitly create a database. A database is created automatically when you first store data in it.

  1. Switch to a database:
   use myDatabase
Enter fullscreen mode Exit fullscreen mode

If the database does not exist, MongoDB creates it when you add data.

  1. Verify creation: Add a collection and document, then use show dbs to list databases. Empty databases won’t appear.

2. What is the insertOne method in MongoDB?

The insertOne method inserts a single document into a collection.

Syntax:

db.collection.insertOne(document)
Enter fullscreen mode Exit fullscreen mode

Example:

db.users.insertOne({ name: "Alice", age: 25 })
Enter fullscreen mode Exit fullscreen mode
  • Adds a document to the users collection.
  • Automatically adds an _id field if not provided.

3. How do you insert multiple documents into a collection?

Use the insertMany method to add multiple documents.

Syntax:

db.collection.insertMany([document1, document2, ...])
Enter fullscreen mode Exit fullscreen mode

Example:

db.users.insertMany([
  { name: "Bob", age: 30 },
  { name: "Charlie", age: 22 }
])
Enter fullscreen mode Exit fullscreen mode
  • Ensures all or none of the documents are inserted, depending on the write concern.

4. Explain the find method in MongoDB.

The find method retrieves documents from a collection based on a query.

Syntax:

db.collection.find(query, projection)
Enter fullscreen mode Exit fullscreen mode
  • Query: Specifies the selection criteria.
  • Projection: Specifies which fields to include/exclude.

Examples:

  1. Find all documents:
   db.users.find({})
Enter fullscreen mode Exit fullscreen mode
  1. Find documents with criteria:
   db.users.find({ age: { $gt: 20 } })
Enter fullscreen mode Exit fullscreen mode
  1. Use projection to include only the name field:
   db.users.find({}, { name: 1, _id: 0 })
Enter fullscreen mode Exit fullscreen mode

5. What is the difference between findOne and find?

  • findOne: Returns the first document that matches the query.
  • find: Returns a cursor to all matching documents.

Examples:

  1. findOne:
   db.users.findOne({ age: { $gt: 20 } })
Enter fullscreen mode Exit fullscreen mode
  • Returns the first document where age > 20.
  1. find:
   db.users.find({ age: { $gt: 20 } })
Enter fullscreen mode Exit fullscreen mode
  • Returns all documents where age > 20.

6. How do you update a document in MongoDB?

Use the updateOne or updateMany methods.

Syntax:

db.collection.updateOne(filter, update, options)
db.collection.updateMany(filter, update, options)
Enter fullscreen mode Exit fullscreen mode

Example:

Update the age of a user named "Alice":

db.users.updateOne({ name: "Alice" }, { $set: { age: 26 } })
Enter fullscreen mode Exit fullscreen mode

7. What is the difference between updateOne and updateMany?

  • updateOne: Updates the first document that matches the filter criteria.
  • updateMany: Updates all documents that match the filter criteria.

Examples:

  1. updateOne:
   db.users.updateOne({ age: { $gt: 20 } }, { $set: { verified: true } })
Enter fullscreen mode Exit fullscreen mode
  1. updateMany:
   db.users.updateMany({ age: { $gt: 20 } }, { $set: { verified: true } })
Enter fullscreen mode Exit fullscreen mode

8. How do you delete documents from a collection?

Use deleteOne or deleteMany to remove documents.

Syntax:

db.collection.deleteOne(filter)
db.collection.deleteMany(filter)
Enter fullscreen mode Exit fullscreen mode

Example:

Delete all users with age > 30:

db.users.deleteMany({ age: { $gt: 30 } })
Enter fullscreen mode Exit fullscreen mode

9. Explain the deleteOne and deleteMany methods.

  • deleteOne: Removes the first document that matches the filter criteria.
  • deleteMany: Removes all matching documents.

Examples:

  1. deleteOne:
   db.users.deleteOne({ name: "Alice" })
Enter fullscreen mode Exit fullscreen mode
  1. deleteMany:
   db.users.deleteMany({ age: { $gt: 25 } })
Enter fullscreen mode Exit fullscreen mode

10. What is the drop method used for?

The drop method is used to delete an entire collection or database.

Collection Drop:

db.collection.drop()
Enter fullscreen mode Exit fullscreen mode
  • Deletes all documents and the collection itself.

Database Drop:

db.dropDatabase()
Enter fullscreen mode Exit fullscreen mode
  • Deletes the database, including all its collections.

Use with caution, as dropped data cannot be recovered.


These operations cover essential CRUD and administrative tasks in MongoDB.

Hi, I'm Abhay Singh Kathayat!
I am a full-stack developer with expertise in both front-end and back-end technologies. I work with a variety of programming languages and frameworks to build efficient, scalable, and user-friendly applications.
Feel free to reach out to me at my business email: kaashshorts28@gmail.com.

Top comments (0)