How to query a collection based on unique id and get documents based on last week or last 12 months?

Viewed 40

I have a collection where a unique document is created/updated everyday based on some calculations. My question is how to get last 7 days document by passing uniqueID and week to the query. And then if I pass uniqueID and months, I get to see last 12 months data grouped by year. I am new to all these kindly bear with me. My collection has the following schema:

const analyticSchema = new Schema(
  {
    uID: String,           // unique for every document
    todaysTimeStamp: Number,  // epoch time for 12:00 am 
    animalsRescued: Number
  },
  {
    timestamps: true
  }
);

Example of my collection:

{ 
    "_id" : ObjectId("6213fd56rg85a3b9f7996b19"), 
    "uID" : "20222022", 
    "todaysTimeStamp" : NumberInt(1645036200), 
    "__v" : NumberInt(0), 
    "createdAt" : ISODate("2022-02-17T17:55:16.616+0000"), 
    "animalsRescued" : NumberInt(10), 
    "updatedAt" : ISODate("2022-02-17T17:55:16.616+0000")
}

How to query all documents for last 12 months grouped by months such as:

{
  Jan: [
         { 
            "_id" : ObjectId("6213fd56rg85a3b9f7996b19"), 
            "uID" : "20222022", 
            "todaysTimeStamp" : 1645036200, 
            "__v" : 0, 
            "createdAt" : ISODate("2022-01-17T17:55:16.616+0000"), 
            "animalsRescued" : 15, 
            "updatedAt" : "2022-01-17T17:55:16.616+0000"
        },

        {
            ..
            ..

         }


0 Answers
Related