mongo aggregate add number of day with date field

on

To add a number of days to a date field in MongoDB using the aggregation framework, you can use the following code as an example:

<code class="">db.collection.aggregate([
   {
      $addFields: {
         newDate: { $add: [ "$dateField", 1000 * 60 * 60 * 24 * NUMBER_OF_DAYS ] }
      }
   }
])</code>

In this code, $addFields is the stage in the pipeline that adds a new field to the documents being processed. $add is used to perform arithmetic operations, and it takes an array of values as arguments, with the first argument being the date field ($dateField in this example) and the second argument being the number of milliseconds equivalent to the number of days you want to add (1000 * 60 * 60 * 24 * NUMBER_OF_DAYS).

Posted in Cocktail by .

About Gowri

I am professional web developer with 8+ years experience. PHP, jQuery, WordPress, Angular and Ionic are my key skills in web development. I am working with strong enthusiastic team with spirit. We provide all web related solution like HTML/CSS development, Web graphic design and Logo.