
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
MongoDB query to search date records using only Month and Day
To search using month and day only, use $where. Let us create a collection with documents −
> db.demo181.insertOne({"ShippingDate":new ISODate("2020-01-10")}); { "acknowledged" : true, "insertedId" : ObjectId("5e398a699e4f06af551997fe") } > db.demo181.insertOne({"ShippingDate":new ISODate("2019-12-11")}); { "acknowledged" : true, "insertedId" : ObjectId("5e398a729e4f06af551997ff") } > db.demo181.insertOne({"ShippingDate":new ISODate("2018-01-10")}); { "acknowledged" : true, "insertedId" : ObjectId("5e398a7d9e4f06af55199800") } > db.demo181.insertOne({"ShippingDate":new ISODate("2020-10-12")}); { "acknowledged" : true, "insertedId" : ObjectId("5e398a879e4f06af55199801") }
Display all documents from a collection with the help of find() method −
> db.demo181.find();
This will produce the following output −
{ "_id" : ObjectId("5e398a699e4f06af551997fe"), "ShippingDate" : ISODate("2020-01-10T00:00:00Z") } { "_id" : ObjectId("5e398a729e4f06af551997ff"), "ShippingDate" : ISODate("2019-12-11T00:00:00Z") } { "_id" : ObjectId("5e398a7d9e4f06af55199800"), "ShippingDate" : ISODate("2018-01-10T00:00:00Z") } { "_id" : ObjectId("5e398a879e4f06af55199801"), "ShippingDate" : ISODate("2020-10-12T00:00:00Z") }
Following is the query to search with Month and Day −
> db.demo181.find({$where : function() { return this.ShippingDate.getMonth() == 1 || this.ShippingDate.getDate() == 10} })
This will produce the following output −
{ "_id" : ObjectId("5e398a699e4f06af551997fe"), "ShippingDate" : ISODate("2020-01-10T00:00:00Z") } { "_id" : ObjectId("5e398a7d9e4f06af55199800"), "ShippingDate" : ISODate("2018-01-10T00:00:00Z") }
- Related Articles
- MongoDB query to search for Month and Day only?
- MongoDB Query to search for records only in a specific hour?
- MySQL query to update only month in date?
- Compare only day and month with date field in MySQL?
- How to display first day and last day of the month from date records in MySQL?
- MySQL query to subtract date records with week day and display the weekday with records
- MongoDB query to get specific month|year (not date)?
- How to compare Year, Month and Day in a MySQL query and display matching records
- Fetch date records comparing with the current date’s day and month in MySQL
- MongoDB query to get date records in a range
- MySQL query to extract only the day instead of entire date
- Format MySQL date and convert to year-month-day
- MongoDB query to convert the field value and create datetime day of month during projection?
- MongoDB query to fetch date records (ISODate format) in a range
- MySQL query to order by current day and month?

Advertisements