[SERVER-37713] Views - Date objects are evaluated at time of CreateView(), not time of Query Created: 23/Oct/18  Updated: 06/Dec/22  Resolved: 26/Oct/18

Status: Closed
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: 4.1.4
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Robert Christ Assignee: Backlog - Query Team (Inactive)
Resolution: Duplicate Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-23656 Current timestamp variable for aggreg... Closed
Assigned Teams:
Query
Operating System: ALL
Steps To Reproduce:

db.createView('viewName', 'collectionName', [{$match: { "SomeDateTimeColumn" :

{ "$gte" : new Date() }

}}])

new Date() will be the time of the createView call.

There appears to be no way to create a view that references the date and time the view is queried.  Ie) db.createView('GetItemsWithFutureDate', 'Collection', [\{$match: {$get: Date_Time_At_Moment_Of_Querying_View}}])

Participants:

 Description   

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



 Comments   
Comment by Charlie Swanson [ 26/Oct/18 ]

We plan to address this problem via SERVER-23656.

Generated at Thu Feb 08 04:46:47 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.