0

I have a mongodb data such as below:

{
   "program" : "DATE TRAINING",
   "durationStart" : "2014-12-20",
   "durationEnd" : "2014-12-20",
   "status" : "Approved"
}

I'd like to count how many data is durationEnd date is less than today date. From the data above I know I have one.

Using nodejs I have tried with the below code but no luck so far. My console.log output for count is 0.

var today = new Date();
collection.count({
            $or: [{
                status: "Pending " + config.TD
            }, {
                status: "Approved",                                  
                durationEnd : {$lte : today }
            }],
             evaluationFlag: null
        }, function(err, count) {
        console.log("count is " + count);
      }
   });
3
  • Your example document, does not have a username field, hence it is evaluated to null during the query, which most likely does not match req.user.username, hence the number of returned documents is 0. Commented Dec 24, 2014 at 8:02
  • sorry i have removed req.user.username...even the updated one the returned document is still 0... Commented Dec 24, 2014 at 8:04
  • 1
    Ok, after the edit which removed the username: your dates in the documents are Strings, which don't match a date. Change them to ISODates. Commented Dec 24, 2014 at 8:04

1 Answer 1

2

Your dates are string not real Date:

use this:

var date = new Date();
var today = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();

collection.count({
        $or: [{
            status: "Pending " + config.TD
        }, {
            status: "Approved",
            durationEnd : {$lte : today }
        }],
         evaluationFlag: null
    }, function(err, count) {
    console.log("count is " + count);
  }
});
Sign up to request clarification or add additional context in comments.

1 Comment

However, I'd strongly suggest saving dates as ISODates instead of strings, since internally the comparison will be an integer comparison (msecs since epoch) rather than a string comparison, which is more costly and therefor slower.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.