Get count of items that matches a condition from mongo db in node js

128
January 14, 2021, at 4:40 PM

I am new to mongo db. I have a scenario where I need to check into collection and the count of items having gameDate = current date. I am using the following query in node js.

const count = this.games.find({gameDate:currentDate}).count();
console.log(count + "items present")

I have 1 matching record in database. But instead of getting count am getting the following error in console.

function(filter, callback) {
  this.op = 'count';
  if (typeof filter === 'function') {
  callback = filter;
  filter = undefined;
  }
 filter = utils.toObject(filter);
 if (mquery.canMerge(filter)) {
  this.merge(filter);
  }
  if (!callback) {
   return this;
  }
  this.exec(callback);
  return this;
 }

How can I get the count here.

Answer 1

You can try the countDocuments function.

games.countDocuments({gameDate:currentDate}, function (err, count) {
    if (err){
        console.log(err)
    }else{
        console.log("Total:", count)
    }
});
READ ALSO
“Duplicate entry” even though the column has UNIQUE constraint

“Duplicate entry” even though the column has UNIQUE constraint

I'm trying to drop some columns which I'm no longer using in my tableI've got a single column with a UNIQUE constraint

96
Run multiple processes in single celery worker on a machine with single CPU

Run multiple processes in single celery worker on a machine with single CPU

I am researching on Celery as background worker for my flask applicationThe application is hosted on a shared linux server (I am not very sure what this means) on Linode platform

83
Import css file from same non-root directory

Import css file from same non-root directory

I would like to distribute my css between multiple files to remain more organizedBut I want to only have to import my main

92