创建一个取决于当前时间的mongo视图 [英] Creating a mongo view that depends on the current time
问题描述
我有一个具有日期字段的集合,我想创建一个mongo视图,按当前日期过滤所有文档。例如,我希望我的视图包含过去7天的所有文档。
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.
我有一个javascript脚本,可以使用聚合管道创建视图。我用javascript方法 - 新的Date()来写出最近7天的条件:
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
]
}
但是当我执行创建视图的脚本时,mongo计算'new Date()'并创建视图,结果为'new Date()'为ISODate。 现在聚合管道在上次执行脚本时计算视图,而不是按实际当前日期计算。
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
]
}
有什么方法可以获得按当前日期过滤的视图?当前日期的任何聚合方法,如oracle的'sysdate'?每次我想要阅读视图时,我都不想执行重新创建视图的脚本。
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.
推荐答案
看起来像这样功能正在为MongoDB 3.7工作。
https://jira.mongodb.org/browse/SERVER-23656
Looks like this feature is in the works for MongoDB 3.7.
https://jira.mongodb.org/browse/SERVER-23656
这篇关于创建一个取决于当前时间的mongo视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!