Creating, Reading, Updating, and Deleting Documents in MongoDB

MongoDB is a popular NoSQL database that allows developers to store and retrieve data in a flexible and scalable manner. One of the core operations in MongoDB is CRUD, which stands for Create, Read, Update, and Delete. In this article, we will explore how to perform these operations on documents in MongoDB.

Creating Documents

To create a new document in MongoDB, we need to specify the collection we want to insert the document into and provide the data in JSON format. For example, consider a collection called "users" with the following fields: "name", "email", and "age".

To insert a new user into the "users" collection, we can use the insertOne() or insertMany() methods. Here's an example of creating a single document:

db.users.insertOne({
  name: "John Doe",
  email: "johndoe@example.com",
  age: 30
});

This will insert a new document with the specified fields into the "users" collection. If we want to insert multiple documents, we can use the insertMany() method:

db.users.insertMany([
  { name: "Jane Smith", email: "janesmith@example.com", age: 25 },
  { name: "Bob Johnson", email: "bobjohnson@example.com", age: 35 }
]);

Reading Documents

Once documents are inserted into MongoDB, we can easily retrieve them using various query methods. For example, to fetch all documents in a collection, we can use the find() method:

db.users.find();

This will return all documents in the "users" collection. We can also add query conditions to filter the results. For instance, to find all users with age greater than 30, we can use the following query:

db.users.find({ age: { $gt: 30 } });

Updating Documents

To update documents in MongoDB, we can use the updateOne() or updateMany() methods. The updateOne() method allows us to update a single document based on a specified query, while updateMany() can update multiple documents.

For example, let's say we want to update the email of the user with the name "John Doe". We can use the following query:

db.users.updateOne(
  { name: "John Doe" },
  { $set: { email: "john.doe@example.com" } }
);

This will update the email field of the document that matches the query.

Deleting Documents

To delete documents in MongoDB, we can use the deleteOne() or deleteMany() methods. The deleteOne() method removes a single document that matches the specified query, while deleteMany() can remove multiple documents.

For example, to delete the user with the name "Jane Smith", we can use the following query:

db.users.deleteOne({ name: "Jane Smith" });

This will delete the document that matches the query from the "users" collection.

Conclusion

In this article, we have explored the basic CRUD operations in MongoDB: creating, reading, updating, and deleting documents. These operations form the foundation of data manipulation in MongoDB and allow developers to build powerful applications with ease. Understanding these operations is essential for anyone working with MongoDB and enables efficient data handling in real-world scenarios.


noob to master © copyleft