Goglides Dev 🌱

Balkrishna Pandey
Balkrishna Pandey

Posted on

How to Work with MongoDB Documents: Insert, Query, Update, and Delete

Introduction

MongoDB is a popular NoSQL database that uses a document-oriented data model. In this blog post, we'll cover the basics of MongoDB documents and how to work with them in the database.

What is a Document?

A document is a basic unit of data in MongoDB, similar to a row in a relational database. It consists of one or more fields that store data in a key-value pair format, similar to a JSON object. Documents can be nested and can contain arrays of other documents.

Inserting Documents

To insert a document in MongoDB, we can use the insertOne method or the insertMany method to insert one or many documents, respectively. Here's an example of inserting a single document into a collection called users:

db.users.insertOne({
  name: "John Doe",
  email: "[email protected]",
  age: 30
});
Enter fullscreen mode Exit fullscreen mode

This will insert a new document into the users collection with the fields name, email, and age.

Here is another example of inserting multiple document at once.

db.users.insertMany([
  {
    name: "John Doe",
    email: "[email protected]",
    password: "password123",
    age: 25,
    created_at: new Date(),
    updated_at: new Date()
  },
  {
    name: "Jane Doe",
    email: "[email protected]",
    password: "password456",
    age: 30,
    created_at: new Date(),
    updated_at: new Date()
  },
  {
    name: "Bob Smith",
    email: "[email protected]",
    password: "password789",
    age: 40,
    created_at: new Date(),
    updated_at: new Date()
  }
]);

Enter fullscreen mode Exit fullscreen mode

Querying Documents

To query documents in MongoDB, we can use the find method. The find method returns a cursor that we can use to iterate over the matching documents. Here's an example of querying for all documents in the users collection:

db.users.find();
Enter fullscreen mode Exit fullscreen mode

This will return a cursor that we can iterate over to access each document in the collection.

MongDB Find
We can also pass a query object to the find method to filter the results based on certain criteria. For example, to find all users with the name "John Doe", we can use the following query:

db.users.find({ name: "John Doe" });
Enter fullscreen mode Exit fullscreen mode

This will return a cursor that contains all documents in the users collection where the name field is "John Doe".
MongoDB Find where

Updating Documents

To update a document in MongoDB, we can use the updateOne method or the updateMany method to update one or many documents, respectively. Here's an example of updating a single document in the users collection:

db.users.updateOne(
  { name: "John Doe" },
  { $set: { age: 31 } }
);
Enter fullscreen mode Exit fullscreen mode

This will update the first document in the users collection where the name field is "John Doe", setting the age field to 31. Since we have multiple document matching this name you will see output something similar to this,

MongoDB Update

To update a document in the users collection where the name is "John Doe" and the age is 25, you can use the following query:

db.users.updateOne(
  { name: "John Doe", age: 25 },
  { $set: { age: 30 } }
)
Enter fullscreen mode Exit fullscreen mode

If you want to update multiple documents that meet the criteria, you can use the updateMany method instead of updateOne.

db.users.updateMany(
  { age: { $gte: 29 } },
  { $set: { status: "inactive" } }
);
Enter fullscreen mode Exit fullscreen mode

This will set the status field of all matching documents to "inactive". Note that updateMany updates multiple documents at once, so be careful when using it to avoid unintended updates.

MongoDB Update Many

Deleting Documents

To delete a document in MongoDB, we can use the deleteOne method or the deleteMany method to delete one or many documents, respectively. Here's an example of deleting a single document in the users collection:

db.users.deleteOne({ name: "John Doe" });
Enter fullscreen mode Exit fullscreen mode

This will delete the first document in the users collection where the name field is "John Doe".

To delete multiple documents in a collection based on a filter, you can use the deleteMany() method. Here is an example of how to delete all documents in the users collection where the name is "John Doe"

db.users.deleteMany({ name: "John Doe" })
Enter fullscreen mode Exit fullscreen mode

MongoDb Delet Many

Conclusion

In this blog post, we covered the basics of MongoDB documents and how to work with them in the database. We covered how to insert, query, update, and delete documents using MongoDB methods, as well as how to structure documents to store data in a key-value pair format. With these basics, you can start working with MongoDB and explore its full potential.

Top comments (0)