Creating a mongo view that depends on the current time

时光毁灭记忆、已成空白 提交于 2019-12-02 05:51:29

问题


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.


回答1:


Looks like this feature is in the works for MongoDB 3.7.
https://jira.mongodb.org/browse/SERVER-23656



来源:https://stackoverflow.com/questions/48626165/creating-a-mongo-view-that-depends-on-the-current-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!