
- 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 get specific month|year (not date)?
You can use aggregate framework along with $month projection operator. Let us first create a collection with documents −
> db.specificMonthDemo.insertOne({"StudentName":"Larry","StudentDateOfBirth":new ISODate('1995-01-12')}); { "acknowledged" : true, "insertedId" : ObjectId("5cb9a9ca8f1d1b97daf71819") } > db.specificMonthDemo.insertOne({"StudentName":"Chris","StudentDateOfBirth":new ISODate('1999-12-31')}); { "acknowledged" : true, "insertedId" : ObjectId("5cb9a9db8f1d1b97daf7181a") } > db.specificMonthDemo.insertOne({"StudentName":"David","StudentDateOfBirth":new ISODate('2000-06-01')}); { "acknowledged" : true, "insertedId" : ObjectId("5cb9a9ee8f1d1b97daf7181b") }
Following is the query to display all documents from the collection with the help of find() method −
> db.specificMonthDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cb9a9ca8f1d1b97daf71819"), "StudentName" : "Larry", "StudentDateOfBirth" : ISODate("1995-01-12T00:00:00Z") } { "_id" : ObjectId("5cb9a9db8f1d1b97daf7181a"), "StudentName" : "Chris", "StudentDateOfBirth" : ISODate("1999-12-31T00:00:00Z") } { "_id" : ObjectId("5cb9a9ee8f1d1b97daf7181b"), "StudentName" : "David", "StudentDateOfBirth" : ISODate("2000-06-01T00:00:00Z") }
Following is the query to get specific month|year, not date −
> db.specificMonthDemo.aggregate([ {$project: {StudentName: 1, StudentDateOfBirth: {$month: '$StudentDateOfBirth'}}}, {$match: {StudentDateOfBirth: 01}} ]).pretty();
This will produce the following output −
{ "_id" : ObjectId("5cb9a9ca8f1d1b97daf71819"), "StudentName" : "Larry", "StudentDateOfBirth" : 1 }
- Related Articles
- MySQL query to fetch date with year and month?
- Java Program to get current date, year and month
- MySQL SELECT query to return records with specific month and year
- MongoDB query to search date records using only Month and Day
- How to get a Date from year, month and day in Java?
- MySQL query to get result by month and year based on condition?
- MongoDB query to get a specific number of items
- MongoDB query to get date records in a range
- How to filter a query on specific date format with MongoDB?
- MongoDB query to get only a specific number of elements
- Get beginning and end date from a specific year in MySQL
- Format MySQL date and convert to year-month-day
- MongoDB query to determine if a specific value does not exist?
- MongoDB query to get only specific fields in nested array documents?
- MySQL query to update only month in date?

Advertisements