Jessie L Smith
Jessie L Smith

Reputation: 17

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.

Upvotes: 0

Views: 1430

Answers (2)

Anish Jain
Anish Jain

Reputation: 550

you can create a stream which will watch for any changes made in your db. Detailed Article - article

Upvotes: 0

PrivateOmega
PrivateOmega

Reputation: 2880

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

Upvotes: 1

Related Questions