Query Documents - CRUD (Create, Read, Update, Delete) Operations
In MongoDB, CRUD operations are used to work with documents stored in a database. In this page, we will explore how to query documents in MongoDB using various methods, including projection, sorting, and filtering.
Syntax
To query documents in MongoDB, you can use the find()
method on a collection object and pass in a query document as a parameter. Here's the general syntax for the find()
method:
db.collection.find(query, projection)
The query
parameter specifies the query document, and the projection
parameter is an optional parameter that specifies the fields to include or exclude in the result documents.
Example
Suppose we have a collection called products
with the following documents:
{
"_id": 1,
"name": "Product 1",
"category": "Category A",
"price": 10.99,
"created_at": "2022-06-01"
}
{
"_id": 2,
"name": "Product 2",
"category": "Category B",
"price": 25.99,
"created_at": "2022-05-15"
}
{
"_id": 3,
"name": "Product 3",
"category": "Category A",
"price": 8.99,
"created_at": "2022-06-05"
}
{
"_id": 4,
"name": "Product 4",
"category": "Category C",
"price": 4.99,
"created_at": "2022-05-30"
}
Read Documents
To retrieve all documents in the products
collection, use the find()
method without any parameters:
db.products.find()
This will return all documents in the collection.
Filter Documents
To filter documents in the collection, pass a query document as a parameter to the find()
method. For example, to retrieve all products in the "Category A" category, use the following query:
db.products.find({ "category": "Category A" })
This will return the following documents:
{
"_id": 1,
"name": "Product 1",
"category": "Category A",
"price": 10.99,
"created_at": "2022-06-01"
}
{
"_id": 3,
"name": "Product 3",
"category": "Category A",
"price": 8.99,
"created_at": "2022-06-05"
}
Project Documents
To project only specific fields in the result documents, use the projection
parameter with the find()
method. For example, to retrieve only the name
and price
fields for products in the "Category A" category, use the following query:
db.products.find(
{ "category": "Category A" },
{ "name": 1, "price": 1, "_id": 0 }
)
This will return the following documents:
{
"name": "Product 1",
"price": 10.99
}
{
"name": "Product 3",
"price": 8.99
}
Sort Documents
To sort the result documents, use the sort()
method with the find()
method. For example, to retrieve all products in ascending order of price, use the following query:
db.products.find().sort({ "price": 1 })
This will return the following documents:
{
"_id": 4,
"name": "Product 4",
"category": "Category C",
"price": 4.99,
"created_at": "2022-05-30"
}
{
"_id": 3,
"name": "Product 3",
"category": "Category A",
"price": 8.99,
"created_at": "2022-06-05"
}
{
"_id": 1,
"name": "Product 1",
"category": "Category A",
"price": 10.99,
"created_at": "2022-06-01"
}
{
"_id": 2,
"name": "Product 2",
"category": "Category B",
"price": 25.99,
"created_at": "2022-05-15"
}
Update Documents
To update a document in the collection, use the updateOne()
method with a filter and an update document. For example, to update the price of "Product 1" to 12.99, use the following query:
db.products.updateOne({ "name": "Product 1" }, { "$set": { "price": 12.99 } })
This will modify the "Product 1" document as follows:
{
"_id": 1,
"name": "Product 1",
"category": "Category A",
"price": 12.99,
"created_at": "2022-06-01"
}
Delete Documents
To delete a document from the collection, use the deleteOne()
method with a filter. For example, to delete the "Product 3" document, use the following query:
db.products.deleteOne({ "name": "Product 3" })
This will remove the "Product 3" document from the collection.
Output
Depending on the query and projection parameters, the output of a find()
query can be a list of documents or a single document. The output of update and delete operations will indicate the number of documents affected by the operation.
Explanation
Querying documents in MongoDB involves specifying a query document and selecting the fields to include or exclude. Sorting, filtering, updating, and deleting documents are additional operations that can be performed on a query result.
Use
Querying documents in MongoDB is essential for retrieving and managing the data stored in collections. Knowing how to filter, sort, project, update, and delete documents gives you control over the information that is returned in a query result.
Important Points
- MongoDB supports CRUD operations for managing documents in collections.
- The
find()
method is used to query documents in a collection. - The
projection
parameter is used to specify which fields to include or exclude in the result documents. - The
sort()
method is used to sort the result documents by one or more fields. - The
updateOne()
method is used to update a document in the collection. - The
deleteOne()
method is used to delete a document from the collection.
Summary
In this page, we discussed how to query documents in MongoDB using the find()
method and various options such as projection, sorting, and filtering. We also discussed how to perform CRUD operations on documents in the collection using the updateOne()
and deleteOne()
methods. By doing so, we can identify and manage data in a collection effectively.