Monitor MongoDB for expired document date fields

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})
  })
}

Answer №1

Utilize the remcoder:chronos plugin to create a dynamic time variable and encase your modification within a Tracker.autorun:

let now = Chronos.currentTime();

Tracker.autorun(function(){
  let games = Games.find({ time_next_action: { $lt: now }}).fetch();
  _.each(games, game => {
  /* Do something */
    Games.update(game._id, {'$set': update})
  })
});

Nonetheless the outcome remains quite akin to your original code, just slightly more condensed. Chronos will continuously refresh the dynamic time (modifiable) every second, triggering the tracker to execute a search and apply your modifications.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is causing React Js to fail loading css when switching from anchor tag to link tag?

I am learning React and experimenting with creating a basic static website using HTML templates in version ^18.2.0 of React JS. Everything seems to be functioning correctly, however, I have encountered an issue with page refresh. Specifically, when I repla ...

Is there a way to adjust the position of a Bootstrap 5 dropdown menu in real-time using javascript or jQuery?

Within my web app, I have implemented dynamically generated Bootstrap 5 dropdown menus that can sometimes be extensive. To optimize screen space usage, I am seeking a way to alter the offset computed by Popper.js through javascript. Unfortunately, I have e ...

Tips for displaying a child React component for a specific duration

One of the React components I have is called PopUpBanner, which is utilized to display messages. For instance, in my login component, I employ it in this manner. If an error arises, the bannerMessage state is updated to showcase the message on the banner: ...

Packages starting with @ are found in the Node.js ecosystem

What's the deal with libraries in Node.js starting with @ symbols? Is there a specific convention for this? I feel like I'm missing something obvious here. ...

Utilizing variables as identifiers in React Native programming

Trying to utilize a dynamic name for a property of an object. For instance, within the function provided below, aiming to use the value stored in the variable 'catagoryId' to access the corresponding child element and assign it to the variable c ...

ID of the Radio button that was most recently selected

My table contains multiple rows, each with a Radio button in the first column: <table class="table table-condensed span8" id="AllocationTable"> <thead> <tr> <th title="Entity to Allocate" style="width: 30%"> ...

End of container Bootstrap 4 row with two columns at the edge

I'm currently working on an angular project with bootstrap 4. Within the container, there are two rows. I specifically want one row to always be positioned at the bottom of the container. Despite trying methods like justify-content: flex-end;, botto ...

What are the steps to create a JSON file structured in a tree format?

Can you provide instructions on creating a JSON file in the following tree format? root child1 child11 child2 child21 child22 child3 child31 I am looking to generate a sample JSON file that adheres to the ...

Using Rails' link_to method within Bootstrap modals

I'm trying to implement a voting system where users can vote on different items displayed in a Bootstrap modal. Each item is listed as a button on the index page, and when clicked, it opens up the modal for voting. However, I'm facing challenges ...

How can I utilize the JQuery GetJSON function to retrieve HTML content from an external webpage?

Imagine you're attempting a jQuery ajax request like this: $.ajax({ ... url: http://other-website.com ... }) You probably know that due to the same-origin policy, this request will fail because the URL is for an external domain. But the ...

How to delete a specific element from the DOM using its ID in JavaScript

I am facing a challenge in removing an element from the page dynamically without refreshing using JavaScript. The script I have written successfully deletes the record from the database, but I need assistance in removing the container element based on it ...

Prevent right-clicking on links from a particular domain

Looking to prevent right-clicking on a link? Check out this code snippet: <script type="text/javascript" language="javascript> $(document).ready(function() { $('body').on('contextmenu', 'a', function(e){ ...

the reason for the blurring of shadows in Three.js

Typically, the shadow appears as shown below: https://i.sstatic.net/5L9N2.png However, upon adjusting the parameters of directionalLight.shadow.camera directionalLight.shadow.camera.left = -50 directionalLight.shadow.camera.right = 50 directionalLight.s ...

Is it possible to run my NPM CLI package on CMD without needing to install it globally beforehand?

I've created a new NPM package, complete with its own set of CLI commands. Let's call this package xyz, and let's imagine it's now live on npmjs.com Now, picture a user who installs this package in their project by executing npm insta ...

Using GridFS with Node.js and Mongoose to efficiently store images

Just venturing into the world of Node.js and looking for some guidance on incorporating GridFS into my projects. Does anyone have a sample code snippet demonstrating how to store and fetch binary data like images using both Node.js and Mongoose? And also ...

Ensure that when you click on the next dropdown menu opener button, the previous dropdown menu closes and only the new menu remains open

Take a look at this page first to get an understanding: On this page, I have implemented multiple dropdown menus that should open when clicked on their respective opener buttons (3 bar icon) and close either when clicked again or anywhere else on the page ...

Tutorial on integrating jquery ui's '.droppable' feature with the jQuery '.on' method

I have a main div with three inner divs labeled 1, 2, and 3 as shown below: <div id="main"> <div style="height:30px; background-color:yellow;" class="bnr">Banner</div> <div style="height:30px; background-color:yellow;" class=" ...

Uploading files with ExpressJS and AngularJS via a RESTful API

I'm a beginner when it comes to AngularJS and Node.js. My goal is to incorporate file (.pdf, .jpg, .doc) upload functionality using the REST API, AngularJS, and Express.js. Although I've looked at Use NodeJS to upload file in an API call for gu ...

Ways to retrieve the initial key object received through an AJAX request

I have a form with three dynamic dropdowns. The second dropdown depends on the first one, and the third dropdown depends on the second one. Therefore, selecting an option in the first dropdown will automatically populate the next two dropdowns. Below is t ...

Showing chosen checkbox information using jQuery ajax

When a user enters more than 3 characters into a text box for live search, results are displayed from the database via an AJAX response as checkboxes with names. I want selected checkboxes to display in a separate div below so users can search multiple dat ...