CRUD Operations

  • CRUD operations are basic database actions used to insert new data, find existing records, update stored information, and delete unwanted data from a system.
  • MongoDB CRUD Operations

    CRUD operations are the fundamental actions to work with any database. In MongoDB, these operations are performed on documents within collections.

    Insert & Find

    Insert (Create)

    Insert adds new documents to a collection.

    Example – Insert One Document:

MongoDB Insert Operation (Create Document)

The Insert operation is used to add new documents to a MongoDB collection. In this example, insertOne() creates a single user document with name, email, and age fields in the users collection.

db.users.insertOne({
  name: "Amit",
  email: "amit@gmail.com",
  age: 25
});
  • Example – Insert Many Documents:

MongoDB Insert Many Documents

The insertMany() method adds multiple documents to a MongoDB collection in a single operation, making bulk data insertion efficient.

db.users.insertMany([
  { name: "Neha", email: "neha@gmail.com" },
  { name: "Rahul", email: "rahul@gmail.com" }
]);
  • Explanation:

    • insertOne() adds a single document

    • insertMany() adds multiple documents at once

    Find (Read)

    Find retrieves documents from a collection.

    Example – Find All Users:

MongoDB Find Operation (Read Data)

The find() operation is used to retrieve documents from a MongoDB collection. In this example, db.users.find() fetches all user records stored in the users collection.

db.users.find();
  • Example – Find with Condition:

Find Documents with Condition in MongoDB

This query retrieves all documents where the age is greater than 20 using a conditional filter.

db.users.find({ age: { $gt: 20 } });
  • Update

    Update modifies existing documents in a collection.

    Example – Update One Document:

MongoDB Update One Document

The updateOne() operation modifies an existing document in a collection. In this example, it updates Amit’s age to 26 using the $set operator.

db.users.updateOne(
  { name: "Amit" },
  { $set: { age: 26 } }
);
  • Example – Update Many Documents:

MongoDB Update Many Documents

The updateMany() operation updates multiple documents in a collection at once. In this example, it sets the status to "Young" for all users whose age is less than 25.

db.users.updateMany(
  { age: { $lt: 25 } },
  { $set: { status: "Young" } }
);
  • Explanation:

    • $set updates or adds a field

    • updateOne() updates the first matched document

    • updateMany() updates all matched documents

    Delete

    Delete removes documents from a collection.

    Example – Delete One Document:

MongoDB Delete One Document

The deleteOne() operation removes a single document from a MongoDB collection. In this example, it deletes the user whose name is "Rahul".

db.users.deleteOne({ name: "Rahul" });
  • Example – Delete Many Documents:

MongoDB Delete Many Documents

The deleteMany() operation removes multiple documents from a MongoDB collection. In this example, it deletes all users with the status "Inactive".

db.users.deleteMany({ status: "Inactive" });
  • Explanation:

    • deleteOne() removes the first matched document

    • deleteMany() removes all matched documents