Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I remove an entire object from an array by its expiry time in mongoose?

Doc:

        "notifcations": [
        {
          "title": "Joined the Tournament.",
          "description": "You have registered in the Tournament of Tournament #142, issued ₹10 from your balance.",
          "amount": 10,
          "createdAt": "2022-05-08T12:03:02.431Z",
          "expiresAt": "2022-05-09T12:03:05.983Z",
          "_id": "6277b17968729c2811137fb0"
        },
        {
          "title": "Joined the Tournament.",
          "description": "You have registered in the Tournament of Tournament #143, issued ₹10 from your balance.",
          "amount": 10,
          "createdAt": "2022-05-08T12:03:02.431Z",
          "expiresAt": "2022-05-09T12:03:05.983Z",
          "_id": "6277b17968729c2811137fb0"
        }
      ]

Here, in the notifications array I want to remove all objects that are expired – "expiresAt" in object. Like to remove all object in which are expiresAt < Date.now()

Notifications Array is stored in mongodb and I want to create a function for this to remove expired Notifications.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Any Help Please!?

>Solution :

Probably the simplest method would be to use Array.filter()

const myObj = {
      "notifcations": [
        {
          "title": "Joined the Tournament.",
          "description": "You have registered in the Tournament of Tournament #141, issued ₹10 from your balance.",
          "amount": 10,
          "createdAt": "2021-05-08T12:03:02.431Z",
          "expiresAt": "2021-05-08T12:03:05.983Z",
          "_id": "6277b17968729c2811137fb0"
        },
        {
          "title": "Joined the Tournament.",
          "description": "You have registered in the Tournament of Tournament #142, issued ₹10 from your balance.",
          "amount": 10,
          "createdAt": "2022-05-08T12:03:02.431Z",
          "expiresAt": "2022-05-09T12:03:05.983Z",
          "_id": "6277b17968729c2811137fb0"
        },
        {
          "title": "Joined the Tournament.",
          "description": "You have registered in the Tournament of Tournament #143, issued ₹10 from your balance.",
          "amount": 10,
          "createdAt": "2022-05-08T12:03:02.431Z",
          "expiresAt": "2022-05-09T12:03:05.983Z",
          "_id": "6277b17968729c2811137fb0"
        }
      ]
};

myObj.notifcations = myObj.notifcations.filter(a => new Date(a.expiresAt) > new Date());
console.log(myObj);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading