Search code examples
mongodbmongoosemongodb-query

Mongodb Last 3 months data. Example: 2024's (March,April,May) and also 2023's(November,December) + 2024's (January)


I am trying to see last three months data, but my code gives me all data. It doesnt give last three months data.

You can see the result in both cases i am getting data from 2023. Ex: "2023-12-12T00:36:52.606Z"

Backend: controler.js This doesn't work.

    let sixMonths = new Date();
      const invoices = await Invoice.find({
        createdAt: { $gte: sixMonths.setMonth(sixMonths.getMonth() - 6) },
      });

Gives:All data

Result:

[
        "2024-08-04T23:31:21.226Z",
        "2024-08-05T00:42:49.717Z",
        "2022-08-05T01:11:20.363Z",
        "2024-07-05T01:11:58.174Z",
        "2024-01-05T01:11:59.248Z",
        "2024-08-05T01:12:00.338Z",
        "2024-08-05T01:54:10.774Z",
        "2024-08-05T01:54:35.970Z",
        "2024-02-05T02:48:05.768Z",
        "2024-06-12T00:36:25.598Z",
        "2024-07-12T00:36:25.815Z",
        "2024-07-12T00:36:26.364Z",
        "2024-08-12T00:36:51.954Z",
        "2024-08-12T00:36:52.109Z",
        "2024-07-12T00:36:52.264Z",
        "2024-01-12T00:36:52.433Z",
        "2023-12-12T00:36:52.606Z",
        "2024-04-12T00:36:52.780Z",
        "2024-05-12T00:36:53.005Z",
        "2024-08-12T00:38:26.562Z",
        "2024-07-31T00:38:27.675Z",
        "2024-08-12T23:20:11.702Z"
    ]

** Backend: controler.js ** This doesn't work.

   var threeMonthsAgo = new Date();
      threeMonthsAgo.setMonth(threeMonthsAgo.getMonth() - 3);
      var threeMonthsAgoInSeconds = threeMonthsAgo.getTime() / 1000;

    const invoices = await Invoice.find({
        createdAt: { $gte: threeMonthsAgoInSeconds },
      });

Gives:All data

Result:

[
        "2024-07-05T01:11:58.174Z",
        "2024-01-05T01:11:59.248Z",
        "2024-02-05T02:48:05.768Z",
        "2024-08-11T15:34:43.434Z",
        "2024-06-12T00:36:25.598Z",
        "2024-07-12T00:36:25.815Z",
        "2024-08-12T00:36:26.018Z",
        "2024-08-12T00:36:26.204Z",
        "2024-07-12T00:36:26.364Z",
        "2024-08-12T00:36:51.954Z",
        "2024-08-12T00:36:52.109Z",
        "2024-07-12T00:36:52.264Z",
        "2024-01-12T00:36:52.433Z",
        "2023-12-12T00:36:52.606Z",
        "2024-04-12T00:36:52.780Z",
        "2024-07-16T01:11:18.913Z",

    ]

Invoice.model.js

import mongoose from "mongoose";
const { ObjectId } = mongoose.Schema;

const invoiceSchema = mongoose.Schema(
  {
    customerName: {
      type: String,
    },
  },
  { timestamps: true }
);
const Invoice = mongoose.model("invoice", invoiceSchema);
export default Invoice;

This is how data is saved in mongodb:

{
  "_id": {
    "$oid": "66b96341d443780c300af038"
  },
  "customerName": "Mehdi",
  "createdAt": {
    "$date": "2024-08-12T01:20:01.383Z"
  },
  "updatedAt": {
    "$date": "2024-08-12T01:20:01.383Z"
  },
  "__v": 0
}

If i want last 3 months like this 2023's(November,December) + 2024's (January) how do i do that????

I am trying to get MongoDB last 3 months data, but failed in every thing and every way. Need help on that.


Solution

  • Your query works when you are trying to fetch the documents created within the latest 3 months.

    However, if you want to query the document from a specific range (For example: from November 2023 to January 2024), you must apply the filter with the start date ($gte) and end date ($lt).

    const invoices = await Invoice.find({
      createdAt: {
        $gte: ISODate("2023-11-01"),
        $lt: ISODate("2024-02-01")
      }
    })
    

    Demo @ Mongo Playground

    I am not sure how you define the start date, assume that it is from the user input or your specified value.

    var endDate = new Date();
    endDate = new Date(endDate.setDate(1));  // Result: 2024-08-01
    
    var startDate = new Date(endDate.setMonth(endDate.getMonth() - 3));  // Result: 2024-05-01
    
    const invoices = await Invoice.find({
      createdAt: {
        $gte: startDate,
        $lt: endDate
      }
    })