 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 }Advertisements
 