I have a collection that has a date field and I want to create a mongo view that filter all the documents by the current date. For example, I want my view to contain all the documents of the last 7 days.
I have a javascript script that creates the view with aggregation pipeline. I used javascript method- new Date() to write the condition of the last 7 days:
{
"$lt": [
{"$subtract": [new Date(), "$DateOfDocument"]}, // difference in milliseconds
1000 * 60 * 60 * 24 * 7 // 7 days in milliseconds
]
}
But when I execute the script that creates the view, mongo calculates 'new Date()' and than creates the view, with the result of 'new Date()' as ISODate. Now the aggregation pipeline calculates the view by the last time I executed the script, not by the actual current date.
{
"$lt": [
{"$subtract": [ISODate("2018-02-05T06:52:32.10+0000"), "$DateOfDocument"]},
604800000
]
}
Is there any way to get a view filtered by the current date? Any aggregation method for the current date, like oracle's 'sysdate'? I don't want to execute the script that recreate the view every time I want to read the view.
Looks like this feature is in the works for MongoDB 3.7.
https://jira.mongodb.org/browse/SERVER-23656