Changing MongoDB item value 2 days after created - javascript

I have a NodeJS application that saves form data in MongoDB database collection. I want to run a function that will change some values of the object in MongoDB database collection 2 days after the form data is saved in the database. I am not sure if it is possible to use setTimeOut() for 2 days, and even if it is possible, I think there must be a better way to do this because form data is saved upon a request.
My alternative solution is creating setInterval which will run the code once a day to check if there are any database items with dates past but I am still looking for a better solution
Thanks for your time!

It is absolutely possible with setTimeOut and setInterval. It will be better if you node package like Node Cron or Node Schedule.

Related

Is there an alternative to a TTL Index in MongoDB that won’t permanently delete documents?

I am building a job board using Node.JS / MongoDB. After a job listing is purchased by the user, it is added to the database and, using a TTL index, it deletes after 30 days. I’m wondering if there’s a way to change a field vs. deleting the entire document? I ask because I would want to give the user the option to “renew” their listing after the expiration period. What would be the best way to approach this?
You can add a field called "renewalDate", initialize it to the create date and then query items that have a renewalDate that is less than 30 days back from now for items to display. Then, to "renew" a listing, you just set the renewalDate to a more current date so it will appear in the query again.
You could then run a periodic task (once a night or once a week) to permanently delete any documents that are old enough that they aren't even eligible for renewal any more. Or you could use the TTL feature to manage this.

architecture for get and store api request data

This is more of a architectural questions. An external platform had product and price information for let's say, books. There is an API available to get this information.
What I read is that it should be possible to create a function in Javascript and connect the Javascript to a page where you want to show the data on my own website. This would mean that for each page request an API-call is made. Since the requested information only changes once a day maximum this does not sound the most efficient solution.
Can someone advise a better solution? Something into the direction of a similar php or javascript function that does the request on the background, schedule an update and import the data into mysql? If so, what language would be most common.
I need the solution for a Joomla/php/mysql environment
Here's a simple idea - fetch and store results from the API (ones you think aren't gonna change in a day), either on disk, or in the database, and later use these stored results to retrieve what you otherwise would've fetched from the API.
Since storing anything in frontend JS across page reloads isn't easy, you need to make use of PHP for that. Based on what's given, you seem to have two ways of calling the API:
via the frontend JS (no-go)
via your PHP backend (good-to-go)
Now, you need to make sure your results are synced every (say) 24 hours.
Add a snippet to your PHP code that contains a variable $lastUpdated (or something similar), and assign it the "static" value of the current time (NOT using time()). Now, add a couple of statements to update the stored results if the current time is at least 24 hours greater than $lastUpdated, followed by updating $lastUpdated to current time.
This should give you what you need with one API call per day.
PS: I'm not an expert in PHP, but you can surely figure out the datetime stuff.
It sounds like you need a cache, and you're not the first person to run into that problem - so you probably don't need to reinvent the wheel and build your own.
Look into something like Redis. There's an article on it available here as well: https://www.compose.com/articles/api-caching-with-redis-and-nodejs/

SetTimeout or use database to track next function trigger?

I have an app where I'm updating data from various APIs and storing it locally. The data in each API is updated at different intervals...3 seconds, 15 seconds, and 30+ seconds.
At the moment, I'm updating each API and then setting a setTimeout to schedule the next update. It works...but is this optimal?
Another option I've considered is to include a field named nextUpdate in my database model that takes a Number (Unix timestamp), and then query the database once per second for any objects that are scheduled to update with mongoose, such as .find({ nextUpdate: { $gt: Date.now() / 1000 }). My concern was that this would cause too many unnecessary calls (and frankly this is my first app so I don't know how many mongo requests per second is considered too much). I'm currently using Mlab as my database host.
So would you continue using setTimeout? The database refresh option I've proposed above? Or another solution?
Thanks in advance for your time and advice.
I would keep using the first approach, though setInterval would be more fitting here.
The second approach seems like it only has downsides to it, unless I'm missing something?
(I would have liked to post this as a comment but cannot post comments yet)

mongoDB use timestamp from _id to evaluate whether its been accessed within last 24 hours

first time posting something, until now lurking always brought some answers. I hope i can also give back this way, but just started out programming a few months ago.
so here we go: I run a website without a Database backend yet and want to add some functionality. The website is currently running on a vps with nodejs and nginx. The database JSON files should look like that:
{account:"1241423",
counter:0,
_id:"someid"}
evertime someone passes me an account number through the webform i want to check if its inside the DB, no problem with that. When i find it i also want to check when its last been accessed (using the timestamp of the _id), if it was more or less than 24 hours ago. if its been more than 24 hours ago i want to increment the counter by 1 and also want to give it a new _id so the time of access gets updated as well. I went through some of the documentation and a few articles and yt videos but didnt quite find what i was looking for.
so lets say i save the JSON file to a temporary js object called "accountInfo":
how do i check whether the creation/update date is more or less than 24 hours in the past by using the accountInfo._id property?
edit: iam currently using mongodb with nodejs.
ObjectId carries only one timestamp and, normally, it is of its creation (obviously). If you want to check when an item was last updated (or accessed), you have to have separate fields for this, and also write code in your app that keeps these timestamps accurate (touches updated_at when you update a document, etc.)
You can extract the timestamp portion by using getTimestamp() method
my_accountinfo._id.getTimestamp()

implement an updates from mongoDB

Im developing a client-server real-time program and I need my server to be up-to-date always. Unill now I have been implement a GET request from the server every X seconds that returns all the entities from the MongoDB.
Now I have big amount of entities and I need to GET only the entities which have been updated since the last GET request.
I think about running sequence in the db for each entity and check every X seconds if the sequens have been increased.
But I will prefer a better way.
Is there any way to get only the recent changes from mongo? Or any nicer architecture ?
You can have a last updated time in the collection. In the client side, you can maintain a last get time.
In the subsequent requests, get all the documents from collection where last updated time is greater than last get time. This way you will get the documents that got updated or inserted since you last get the data (I.e. delta).
Edit:
MongoDB maintains the date object in UTC format. As long as the date at client side is maintained in UTC and send the same data in the subsequent request, it should retrieve the latest updated records.

Categories