
- 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
Getting MongoDB results from the previous monthn
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 Articles
- Java Program to display previous month from GregorianCalendar
- Getting distinct values from object array in MongoDB?
- How to get last day of the previous month in MySQL?
- How to get the first day of the previous month in MySQL?
- Fetch month, day, year, etc. from ISODate in MongoDB?
- Getting calendar for a month in Python
- Get a single element from the array of results by index in MongoDB
- Getting month name instead of numeric month number in report in SAP
- Find current and previous documents in MongoDB
- How to concatenate results in MongoDB?
- Why am I not getting the desired results despite working very hard?
- Push query results into variable with MongoDB?
- How to print results of script in MongoDB?
- Getting the highest value of a column in MongoDB?
- Get all MySQL records from the previous day (yesterday)?

Advertisements