I'm currently working with MongoDB and MeteorJS, and I'm wondering if there is a way to observe the value of a specific field in a document. I know we can observe document updates, creations, and removals, but I am interested in monitoring a field value.
In each of my documents, there is a field called time_next_action
which stores a JavaScript Date value. I want to be able to update each document when this attribute indicates that the time for the next action has passed.
Currently, I have an interval that runs twice per second, but I am facing the following issues:
- It requires a
Collection.find({})
operation on each iteration - The callback function is not always triggered at the exact time, sometimes with a delay of 0 to 500ms.
Here is the code I am currently using, although I am not completely satisfied with it:
Meteor.setInterval(function() {
beginIntervals();
}, 500);
let beginIntervals = function() {
let now = new Date()
let games = Games.find({time_next_action: {'$lt': now}}).fetch()
_.each(games, game => {
/* Do something */
Games.update(game._id, {'$set': update})
})
}