Making sure only the last document is "active" in a collection

I’ve got this little program I’m building that is storing some simple information for a url check to see if the site is reachable (up) or not.

The collection looks like this.

{
	"_id" : "MD5CcGEG92QmCjZfo",
	"urlId" : "yQSs4hMHndpt2Q4fH",
	"url" : "http://samplepage.com",
	"status" : "Up",
	"statusColor" : "#32CD32",
	"nextRun" : "2018-06-09 14:43:24",
	"active" : false,
	"runOn" : ISODate("2018-06-09T19:28:24.612Z"),
	"runFor" : "sample@email.com"
}

Each time I run a set of new checks, I pull the URLs my user has added to be checked from a different collection, along with the URL Id, and go through them in a for loop.

As I get to each one, I check to see if the current date time is greater than or equal to the nextRun date time. If so, then I check the site, and go through some steps.

I use the following code to try and determine if a hostStatus entry (like above) exists and is set to active: true, and if so update those records and make them false, then in the return of the Meteor.call function, call a new function to insert my newest data and set it to active: true so I always only have the last item as active: true in the collection.

I do this so I can filter on active in my publish function, as these checks run on a timer, and the collection grows pretty quickly, and I don’t want the page to load forever waiting on the subscription to be ready. It’s very fast to load 6, or 10, or even 100 items vs. 12000 and more.

So here’s the code I run through.

let currHostStatus = HostStatus.find({ urlId: urlId, active: true }).count();
          console.log("------------- !!!!!!!!!!!!!!!! ----------------- !!!!!!!!!!!!!!! ----------------");
          console.log("Current Host Active Status Count for " + myURL + " is: " + currHostStatus);
          console.log("");

          //
          // if there is one, we set it to active = false, if not, wwe just add this
          // latest status check to the db as active = true.
          //
          if (currHostStatus != 0) {
            console.log("");
            console.log("Found Host Count Active to not be zero!");
            console.log("");
            
            Meteor.call('hostStatus.updateActive', urlId, function(err, result){
              if (err) {
                console.log("Error updating host status: " + err);
              } else {
                let reCheck = HostStatus.find({ urlId: urlId, active: true }).count();

                console.log("!!!!!! -------------- !!!!!! ------------- !!!!!! ---------------");
                console.log("Re-count found Active hosts for url " + myURL + " are: " + reCheck);
                console.log("");

                if (reCheck != 0) {
                  Meteor.call('hostStatus.updateActive', urlId, function(err, result){
                    if (err) {
                      console.log("Error updating host status: " + err);
                    } else {
                      Meteor.call('hostStatus.add', urlId, myURL, status, color, nextCheck, function(err, result) {
                        if (err) {
                          console.log("Error adding host status: " + err);
                        } else {
                          // console.log("Success.");
                        }
                      });
                    }
                });
               } else {
                  console.log("!!!!!! -------------- !!!!!! ------------- !!!!!! ---------------");
                  console.log("Recheck was Zero!");
                  console.log("");

                  Meteor.call('hostStatus.add', urlId, myURL, status, color, nextCheck, function(err, result) {
                    if (err) {
                      console.log("Error adding host status: " + err);
                    } else {
                      // console.log("Success.");
                    }
                  });
                }
                
              }
            });
          } else {
            console.log("!!!!!! -------------- !!!!!! ------------- !!!!!! ---------------");
            console.log("All Host Entries were Non-Active Already!");
            console.log("");
            Meteor.call('hostStatus.add', urlId, myURL, status, color, nextCheck, function(err, result) {
              if (err) {
                console.log("Error adding Status Check for Host: " + err);
              }
            });
          }

I’m looking for a better way to do this. I tried just finding the last record assuming it would be the only active one, and changing it, but it just never worked. I didn’t get as many active ones over time, but definitely had misses now and then.

Now I’m looking to see if any are active, then running an update that changes everything to active: false, then returning and trying to set my newest one active.

I’ve found, however, that some times the insert for the newest active item isn’t happening.

So, I’ve obviously got something happening out of order.

Any help is, as always, greatly appreciated.