MongoDB Query by Date - Retrieve Documents in Date Range
How to Query MongoDB Documents by Date?
In MongoDB, querying documents based on dates can be achieved by specifying conditions for date fields using operators like $gte (greater than or equal to), $lte (less than or equal to), $gt (greater than), and $lt (less than). Date-based queries are commonly used to filter records within a certain date range or to find records before or after a specific date.
Description:
To query MongoDB documents by date, you can use comparison operators in the query filter to specify date conditions. This allows you to retrieve documents from a specific date range or filter documents based on criteria such as before, after, or on a given date.
Syntax:
To filter documents based on a date, use the following syntax:
db.<collection>.find({ <date_field>: { $gte: ISODate("<start_date>"), $lte: ISODate("<end_date>") } })
Explanation:
- <collection>: The collection you are querying.
- <date_field>: The field in the documents that stores date values.
- <start_date> and <end_date>: The start and end dates to filter by, in ISODate format.
Example:
Suppose you have a sales collection with a saleDate field, and you want to retrieve all documents where the sale date is between January 1, 2023, and March 31, 2023.
Code:
// Query to find documents with 'saleDate' between January 1, 2023, and March 31, 2023
db.sales.find({
saleDate: {
$gte: ISODate("2023-01-01T00:00:00Z"), // Start date (inclusive)
$lte: ISODate("2023-03-31T23:59:59Z") // End date (inclusive)
}
})
Explanation:
- db.sales.find(...)
- Calls the find() method on the sales collection to retrieve documents matching the specified date range.
- saleDate: { $gte: ISODate("2023-01-01T00:00:00Z"), $lte: ISODate("2023-03-31T23:59:59Z") }
- Specifies the saleDate field in the query filter.
- $gte: Ensures the saleDate is greater than or equal to January 1, 2023.
- $lte: Ensures the saleDate is less than or equal to March 31, 2023.
By setting both $gte and $lte, this query retrieves documents where the saleDate falls between the start and end dates.
Additional Notes:
- Single Date Filter: You can also filter documents for a single date. For instance, { saleDate: ISODate("2023-01-01T00:00:00Z") } would match documents with exactly that date and time.
- ISODate Format: Always use ISODate format to store and query date values in MongoDB, as it ensures consistency and accurate comparisons.
Example Output:
The result of the query could return documents structured as follows:
[ { "_id": ObjectId("64b6a7d8f1b4"), "saleDate": ISODate("2023-02-15T14:20:00Z"), "item": "Laptop", "price": 1200 }, { "_id": ObjectId("64b6a8e9f2c5"), "saleDate": ISODate("2023-03-10T09:45:00Z"), "item": "Phone", "price": 800 } ]
This output shows documents where the saleDate falls within the specified range.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/mongodb/snippets/how-to-query-mongodb-documents-by-date.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics