如何创建具有可变日期的部分索引? [英] How to create a partial index with variable date?
问题描述
我想在MongoDB中创建部分索引,该索引不包括从当前日期算起过去30天的文档.
I want to create a partial index in MongoDB which excludes documents that are older than 30 days in the past from the current date.
类似这样的东西(伪代码):
Something like this (pseudo code):
partialFilterExpression: { date: { $gte: { $currentDate - 30 days }} }
$currentDate
应该是实际的当前日期,换句话说,日期是一个动态值,而不是静态值.
The $currentDate
should be the actual current date, in other words the date is a dynamic value, not a static value.
有可能吗?
推荐答案
据我所知,部分索引&当前不支持过滤器表达式中的动态值.过滤器表达式仅支持常规mongo查询运算符的子集(并且该子集不包含$date
运算符).
As far as I know, dynamic values in the filter expression are not currently supported with partial indexes & the filter expression only supports a subset of regular mongo query operators (and that subset does not include the $date
operator).
一种近似所需行为的方法是在文档上创建一个shouldIndex
布尔字段,将{shouldIndex: true}
作为您的过滤器表达式,并针对每天早于
One way to approximate your desired behavior is by creating a shouldIndex
boolean field on your documents, have {shouldIndex: true}
be your filter expression, and have a script update that field once a day for documents that are older than current_date - 30
.
这篇关于如何创建具有可变日期的部分索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!