How can I set a delay while saving the data?

hi, I have a device that sends me data every 2 seconds. when the data comes to my node js app I save the data immediately. but I want to save the data every 5 minutes. I tried to use set timeout but it caused memory leaks and so on. can I set a delay to MongoDB schema.

Hi Berat, welcome to the community!

What’s your reason for only wanting to save the data every 5 minutes? e.g., is it to reduce the number of calls to the database, or that you only want the data visible to the app to be updated every 5 minutes?

If it’s the later, then one option would be for your Node app to add the changes to a buffer collection, and then use a scheduled Atlas trigger (or a cron job if not using Atlas) to apply those changes to the “live” collection every 5 minutes.

first of all thanks for answering. My goal is to show data on my dashboard live and save it. if I save data every 2 seconds, nearly more than a hundred devices probably make millions of data, and when it comes to query it is not easy to find data with a 2GB digital ocean server .at the same time I can not make these devices send me data every 5 minutes because I created an alarm system that checks conditions about data which come to my node js server and data has come to me every 2 seconds. I want to get data every 2 seconds and show them on the dashboard but when I want to query sensor data it is gonna a little bit hard for me. I am going to try out the changes to a buffer collection. best regards.

Sounds to me like you want to do something like

sorry for answering late. I still searching for it. in the blog which you have sent, the blogger is talking about taking the data every minute. your answer helped me a lot but not solved my problem. thanks. Best regards

Hey guys I think I found a solution. when data comes, I update the current data of the device sensor and I will make a cronjob that finds the current data of the device sensors and save them in a sensor document as a nested document every 5 minutes. thanks, Steeve Juneau for adding this blog. Sorry for answering late. it took a lot of time to read and understand the concept of that blog because my English level is elementary. thank you all guys again. have a good day. problem solved

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.