Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-37713

Views - Date objects are evaluated at time of CreateView(), not time of Query

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 4.1.4
    • Component/s: Aggregation Framework
    • None
    • Query
    • ALL
    • Hide

      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}}])

      Show
      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}}] )

      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

            Assignee:
            backlog-server-query Backlog - Query Team (Inactive)
            Reporter:
            robc Robert Christ
            Votes:
            1 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: