簡體   English   中英

如何在 mongoose 的到期時間之前從數組中刪除整個 object?

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

文檔:

        "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"
        }
      ]

在這里,在通知數組中,我想刪除所有過期的對象 - object 中的“expiresAt” 。就像刪除所有 object 中的expiresAt < Date.now()

通知數組存儲在 mongodb 中,我想為此創建一個 function 以刪除過期的通知。

有什么幫助嗎??

可能最簡單的方法是使用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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM