I have a query where I need to get events that are one day before or after from a specific date. I need to add or subtract one day to that ISODate variable. Here is my query :
db.event.find().forEach( function (x) {
print("x : " + x.EventID + ", " + x.ISODate);
db.events.find( {
"$or" : [{
"StartDate" : { "$gte" : x.ISODate } // Here i need to subtract one day
}, {
"EndDate": { "$lt" : x.ISODate} // Here i need to add one day
}]
}).forEach(function(otherDay) {
print("x.EventID : " + x.EventID + ", other.Date : " + otherDay.StartDate + " - " + otherDay.EndDate);
});
});
How can i add or subtract days to an ISODate variable in mongodb shell?
Not an exact answer but related.
I needed to increment a date field for all items in the mongodb collection. Use minus if you need to subtract.
The query below will add 1 day to myDateField in myCollection.
This has been answered on Query to get last X minutes data with Mongodb
And in your case, for a number of days:
or
(here the 3 is your number of days)