Search code examples
node.jsmongodb

How to continuously query a mongodb database efficiently


I'm new to Node and MongoDB and I have a seemingly simple request. I've managed to connect to my database, and use a query to get my desired results. Now, I want to have this query continue indefinitely, since the end goal for my project is to plot data real time.

I would have thought a simple 'while (true)' loop would suffice, but that doesn't seem to be the case.

const MongoClient = require('mongodb').MongoClient;

// Connection URL
const url = 'mongodb://<username>:<password>@ds157614.mlab.com:57614/flight_data';


// Use connect method to connect to the Server
MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {

  if (err) throw err;

  var dbo = db.db("flight_data").collection("data");

while(true)
{

    dbo.find().sort({_id: 1}).limit(1).toArray(function(err, result) {

    if (err) throw err;

    console.log("Temperature: " + result[0].data.temperature);

  });

}

db.close();

});

I have found that the while loop is indeed running, but for some reason, the query just doesn't happen when inside the while loop. If you remove the while loop, the code functions fine. I just want it to continually print the results of the query being repeated.


Solution

  • Querying a DB continuously is inefficient and resource wasting, instead use change streams. It watches collection for any changes and will make the db call then only. Works only for Mongo 3.6+.

    const MongoClient = require("mongodb").MongoClient;
    
    // Connection URL
    const url =
      "mongodb://<username>:<password>@ds157614.mlab.com:57614/flight_data";
    
    // Use connect method to connect to the Server
    MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
      if (err) throw err;
    
      const collection = db.collection("data");
      const changeStream = collection.watch();
      changeStream.on("change", next => {
        // process next document
        collection
          .find()
          .sort({ _id: 1 })
          .limit(1)
          .toArray(function(err, result) {
            if (err) throw err;
    
            console.log("Temperature: " + result[0].data.temperature);
          });
      });
      db.close();
    });