Muhammad Aadil Banaras
Muhammad Aadil Banaras

Reputation: 1284

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.

Upvotes: 3

Views: 484

Answers (1)

Xavier Guihot
Xavier Guihot

Reputation: 61766

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") }

Upvotes: 2

Related Questions