Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include the current date in mongo aggregation

I need the current date in a field within a project aggregation stage like this:

$project:{
   todays_date: ''
}

I need some checks on basis of that below, I tried many things but they are not allowing me to add like this:

$project:{
  todays_date: Date(), 
}

but the date is from 1970.

like image 886
Muhammad Aadil Banaras Avatar asked Feb 28 '26 11:02

Muhammad Aadil Banaras


1 Answers

Starting Mongo 4.2, you can use the new aggregation variable $$NOW which provides the current datetime:

db.collection.aggregate({ $project: { todays_date: "$$NOW" } })
// { todays_date: ISODate("2019-07-21T09:05:46.123Z") }
like image 174
Xavier Guihot Avatar answered Mar 02 '26 00:03

Xavier Guihot