0

Nodejs Script:

var numberlist = db.collection('fsnumbers');

var getTotalforToday = function(){
        var curDate = moment().format('MMMM Do YYYY'); 
        curDate = "/"+curDate+"/i";

        numberlist.find({"date": { $regex: curDate } }, function(err, items){
            if(items){
                return items;
            }
        });

}

MongoDB Console Query:

db.fsnumbers.find( { "date": { $regex: /July 7th 2016/i } } )

Result:

{ "_id" : ObjectId("577d45c9b2b0e2e40a9187f1"), "numbers" : 5869437261, "date" : "July 7th 2016, 1:54:17 am" }
{ "_id" : ObjectId("577d45f9b2b0e2e40a9187f2"), "numbers" : 5862389712, "date" : "July 7th 2016, 1:55:05 am" }
{ "_id" : ObjectId("577d4606b2b0e2e40a9187f3"), "numbers" : 3138990309, "date" : "July 7th 2016, 1:55:18 am" }
{ "_id" : ObjectId("577dde97f38da37019eefb8c"), "numbers" : 3137208860, "date" : "July 7th 2016, 12:46:15 pm" }
{ "_id" : ObjectId("577ddea7f38da37019eefb8d"), "numbers" : 2484993963, "date" : "July 7th 2016, 12:46:31 pm" }
{ "_id" : ObjectId("577ddeb4f38da37019eefb8e"), "numbers" : 3136498218, "date" : "July 7th 2016, 12:46:44 pm" }
{ "_id" : ObjectId("577de75749d7317c0e5d2bc7"), "numbers" : 1444444444, "date" : "July 7th 2016, 1:23:35 pm" }
{ "_id" : ObjectId("577debcd3e1652b80f54f030"), "numbers" : 3345343443, "date" : "July 7th 2016, 1:42:37 pm" }
{ "_id" : ObjectId("577dec2ffc8631300d064fee"), "numbers" : 3454334345, "date" : "July 7th 2016, 1:44:15 pm" }
chridam
  • 100,957
  • 23
  • 236
  • 235

2 Answers2

0

/July 7th 2016*/i

Since your dates are strings like July 7th 2016, 1:54:17 am, you need the * at the end of the regex to allow for any string representing the hour following your pattern.

You can find a better, no-regex approach to query dates with moment here: MongoDB/Mongoose querying at a specific date

Community
  • 1
  • 1
marton
  • 1,300
  • 8
  • 16
0
curDate = "/"+curDate+"/i";

You're creating a string there, not a regular expression object.

Try this:

curDate = new RegExp(curDate, 'i');
robertklep
  • 198,204
  • 35
  • 394
  • 381