Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query all data where `created` today (from 00h:00m:00s to now) in mongoose?

Tags:

mongoose

How to query all data where created is today (from 00h:00m:00s to now) in mongoose?

{
  "user": ObjectId("52fe173148ee58a0180c1d77"),
  "caption": "aaaa",
  "created": ISODate("2014-02-14T16:04:57.98Z"),
  "__v": NumberInt(0)
}

{
  "user": ObjectId("52fe173148ee58a0180c1d77"),
  "caption": bbb",
  "created": ISODate("2014-02-14T14:10:27.986Z"),
  "__v": NumberInt(0)
}
like image 909
user3044147 Avatar asked Dec 12 '25 03:12

user3044147


1 Answers

Just create a Date object that contains the start of today and then use that in the query:

var now = new Date();
var startOfToday = new Date(now.getFullYear(), now.getMonth(), now.getDate());
MyModel.find({created_on: {$gte: startOfToday}}, function (err, docs) { ... });
like image 147
JohnnyHK Avatar answered Dec 14 '25 00:12

JohnnyHK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!