

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
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
Getting MongoDB results from the previous month
At first, get the current month and subtract by 1 to fetch previous month records. Let us first create a collection with documents −
> db.findOneMonthAgoData.insertOne({"CustomerName":"Chris","PurchaseDate":new ISODate("2019-12-26")}); { "acknowledged" : true, "insertedId" : ObjectId("5e04e16c150ee0e76c06a04f") } > db.findOneMonthAgoData.insertOne({"CustomerName":"David","PurchaseDate":new ISODate("2019-11-26")}); { "acknowledged" : true, "insertedId" : ObjectId("5e04e178150ee0e76c06a050") } > db.findOneMonthAgoData.insertOne({"CustomerName":"Bob","PurchaseDate":new ISODate("2020-11-26")}); { "acknowledged" : true, "insertedId" : ObjectId("5e04e186150ee0e76c06a051") }
Following is the query to display all documents from a collection with the help of find() method −
> db.findOneMonthAgoData.find();
This will produce the following output −
{ "_id" : ObjectId("5e04e16c150ee0e76c06a04f"), "CustomerName" : "Chris", "PurchaseDate" : ISODate("2019-12-26T00:00:00Z") } { "_id" : ObjectId("5e04e178150ee0e76c06a050"), "CustomerName" : "David", "PurchaseDate" : ISODate("2019-11-26T00:00:00Z") } { "_id" : ObjectId("5e04e186150ee0e76c06a051"), "CustomerName" : "Bob", "PurchaseDate" : ISODate("2020-11-26T00:00:00Z") }
Here is the query to get result from previous month −
> monthData=new Date(); ISODate("2019-12-26T16:43:04.283Z") > monthData.setMonth(monthData.getMonth() - 1); 1574786584283 > db.findOneMonthAgoData.find({PurchaseDate:{$gte:monthData}});
This will produce the following output −
{ "_id" : ObjectId("5e04e16c150ee0e76c06a04f"), "CustomerName" : "Chris", "PurchaseDate" : ISODate("2019-12-26T00:00:00Z") } { "_id" : ObjectId("5e04e186150ee0e76c06a051"), "CustomerName" : "Bob", "PurchaseDate" : ISODate("2020-11-26T00:00:00Z") }
- Related Questions & Answers
- Java Program to display previous month from GregorianCalendar
- Getting distinct values from object array in MongoDB?
- How to get the first day of the previous month in MySQL?
- How to get last day of the previous month in MySQL?
- Getting calendar for a month in Python
- Getting month name instead of numeric month number in report in SAP
- Fetch month, day, year, etc. from ISODate in MongoDB?
- Get a single element from the array of results by index in MongoDB
- Why am I not getting the desired results despite working very hard?
- How to concatenate results in MongoDB?
- Find current and previous documents in MongoDB
- Push query results into variable with MongoDB?
- Getting the highest value of a column in MongoDB?
- Get Month Name from Month number in MySQL?
- Get all MySQL records from the previous day (yesterday)?
Advertisements