-
Type: Bug
-
Resolution: Duplicate
-
Priority: Major - P3
-
None
-
Affects Version/s: 4.1.4
-
Component/s: Aggregation Framework
-
None
-
Query
-
ALL
-
On any version of mongo that supports db.createView() run the following command to create a new view:
db.createView('viewName', 'collectionName', [{$match: { "SomeDateTimeColumn" :
{ "$gte" : new Date() }}}])
Mongo will immediately translate this pipeline to $match: {"SomeDateTimeColumn": {$gte: ISODate("Date_And_Time_At_The_Instant_The_View_Was_Created")}}
As a result, it appears to be impossible to write any view such as:
db.createView('GetItemsWithFutureDate', 'Collection', [\{$match: {$get: Date_Time_At_Moment_Of_Querying_View}}])
This appears to be because this is the same behavior that exists when one runs db.collection.aggregate(pipeline). When running an aggregate query, date time objects in the pipeline are evaluated at the time of the query. Since the moment the aggregate query is evaluated is the same moment (roughly) that the aggregate query is run, this makes sense for aggregation queries.
However, this does not make sense for views. Evaluation of the DateTime object at view creation, means it appears to be impossible to create views that are based on the current date and time.
All attempts to use things such as $dateFromString: "now" do not work.
Things such as $CurrentDate are too restricted for use.
The proposed $$NOW suggestion in SERVER-23656 would potentially solve this problem.
An alternative solution, would be for the createView command to store the aggregation query string, but not evaluate it until the moment the view is actually queried.
This affects all versions of mongo that support views
- duplicates
-
SERVER-23656 Current timestamp variable for aggregate expressions
- Closed