Add day to ISODate in MongoDB


We can use $add an operator to add days in ISODate in MongoDB, $add is the Arithmetic Aggregation Operator, which adds number and date in MongoDB.

Syntax:


{ $add: [ <expression1>, <expression2>, ... ] }


Note:  If one of the argument is date $add operator treats to other arguments as milliseconds to add to the date.

Example: Suppose we have a Test collection as below.


{"Title" : "Add day to ISODate in MongoBD","CreatedDate" : ISODate("2016-07-07T08:00:00.000Z")}


Query to add 2 days in CreatedDate


db.Test.aggregate([
     { $project: { Title: 1, AddedDate: { $add: [ "$CreatedDate", 2*24*60*60000 ] } } }
   ])


Result:


{ "_id" : ObjectId("579a1567ac1b3f3732483de0"), "Title" : "Add day to ISODate in MongoBD", "AddedDate" : ISODate("2016-07-09T08:00:00.000Z") }


Note: As mentioned in the above note, we have to convert days in a millisecond because $add operator treat to another argument as milliseconds if one of the arguments is a date.

Add Minutes

We can add minute same as above, we need to convert minutes into milliseconds, see following query.


db.Test.aggregate([
     { $project: { Title: 1, AddedDate: { $add: [ "$CreatedDate", 10*60*1000 ] } } }
   ])


Result:



{ "_id" : ObjectId("579a1567ac1b3f3732483de0"), "Title" : "Add day to ISODate in MongoBD", "AddedDate" : ISODate("2016-07-07T08:10:00.000Z") }

NUnit : Introduction

There are several unit test tools in the market, but one of them is NUnit, the most popular tool for doing unit testing. So in this tutorial...