我有以下数据:
[
...
{
date: {
startDate: '2017-04-21T00:00:00.000Z',
endDate: '2017-04-21T00:00:00.000Z',
startTime: '12:00',
endTime: '21:00'
}
},
{
date: {
startDate: '2017-04-21T00:00:00.000Z',
endDate: '2017-04-21T00:00:00.000Z',
startTime: '09:00',
endTime: '23:00'
}
}
...
]
这是我到目前为止所拥有的:
export const OngoingEventsQuery: any = {
type: 'event',
body: {
from: 0,
size: 2,
query: {
range: {
'date.startDate': {
gt: new Date().getDate() - 1
},
'date.endDate': {
lt: new Date().getDate() + 1
}
}
}
}
};
现在,我要做的是匹配与今天的日期匹配的对象,并考虑
startTime
和endDate
。因此,如果今天的日期是april 21 2017
而时间是15:00
,则上述两个对象都应匹配。首先尝试在不匹配开始时间和结束时间的情况下,此查询为我提供了数据库中所有可能的对象,这确实是错误的。
我以为我必须给
yesterday
和tomorrow
之间的一个日期,以便通过使用带有加号1或减号的today
来获得getDate()
。但这显然是错误的。我怎样才能做到这一点?
编辑:奇怪的是,如果我从查询中删除
from
和size
,我将获得0个结果。
最佳答案
{
"query": {
"bool": {
"must": [
{
"range": {
"date.startDate": {
"gte": "now/d",
"lte": "now/d"
}
}
},
{
"range": {
"date.endDate": {
"gte": "now/d",
"lte": "now/d"
}
}
}
]
}
}
}
在the documentation中,日期内部保存在UTC中,因此,如果您的比较将使用UTC时间,则应该进行以下操作:
Internally, dates are converted to UTC (if the time-zone is specified) and stored as a long number representing milliseconds-since-the-epoch.
关于typescript - 如何查询ISO日期为今天的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43540165/