英文:
Mongodb query to select all documents between two dates
问题
我目前正在困扰于这个问题,并尝试了这个平台上的多个解决方案,比如这个,但没有找到结果。
基本上,我有一个在nestjs应用程序中的端点,通过POST请求接收一个from
和一个to
参数,它们都是日期(可以使用new Date(from/to)
进行转换的字符串)。
我正在尝试获取数据库中在这两个日期(from
和to
)之间的所有文档。
这是我正在执行的查询:
const data = await model.find( {
payload: {
timestamp: {
$gte: new Date( from ).toISOString(),
$lte: new Date( to ).toISOString()
}
}
} );
我得到的响应是这个错误:
"Cast to date failed for value "{ '$gte': 2023-01-03T14:15:52.767Z, '$lte': 2023-01-03T14:16:52.767Z }" (type Object) at path "timestamp" for model "data"",
我还尝试将timestamp键放在payload对象之外,如下:
带错误的查询:
{ payload:{timestamp: ... }}
查询:
const data = await model.find( {
payload: {
timestamp: {
$gte: new Date( from ).toISOString(),
$lte: new Date( to ).toISOString()
}
}
} );
没有错误,但我每次都得到一个空数组:
{payload:{}, timestamp: ...}
查询:
const data = await model.find( {
timestamp: {
$gte: new Date( from ).toISOString(),
$lte: new Date( to ).toISOString()
}
} );
我到底做错了什么?
1: https://stackoverflow.com/questions/2943222/find-objects-between-two-dates-mongodb
英文:
I am currently struggling with this topic and I tried multiple solution from this platform like this one and found no results.
Basically I have an endpoint in a nestjs application that receives via a POST request a from
and a to
parameter that are both dates(string that can be converted with new Date(from/to)
).
I am trying to get all documents in the database between those two dates(from
and to
).
Here is the query that I am making:
const data = await model.find( {
payload: {
timestamp: {
$gte: new Date( from ).toISOString(),
$lte: new Date( to ).toISOString()
}
}
} );
The response that I get is this error:
"Cast to date failed for value \"{ '$gte': 2023-01-03T14:15:52.767Z, '$lte': 2023-01-03T14:16:52.767Z }\" (type Object) at path \"timestamp\" for model \"data\"",
I also tried to get put the timestamp key outside the payload object so:
With error:
{payload:{timestamp: ... }}
query:
const data = await model.find( {
payload: {
timestamp: {
$gte: new Date( from ).toISOString(),
$lte: new Date( to ).toISOString()
}
}
} );
Without error but I get an empty array every time:
{payload:{}, timestamp: ...}
query:
const data = await model.find( {
timestamp: {
$gte: new Date( from ).toISOString(),
$lte: new Date( to ).toISOString()
}
} );
What exactly am I doing wrong?
答案1
得分: 0
使用此问题的讨论作为线索,问题出在此处的查询语法上。由于您正在使用嵌套字段,查询应该使用点表示法:
const data = await model.find( {
'payload.timestamp': {
$gte: new Date( from ).toISOString(),
$lte: new Date( to ).toISOString()
}
} );
使用当前的语法,Mongoose 正在尝试构建一个日期对象,这导致了“值的日期转换失败...(类型为Object)”错误。无论如何,这种语法都不是您想要的,因为数据库将搜索并与具有不同语义的对象进行比较,这可能与您的预期不符。
补充说明,关于此主题的查询语义的更多详细信息可以在文档中找到。
英文:
Using the discussion on this issue as the clue, the problem is in the query syntax here. Since you are using embedded fields, the query should use dot notation:
const data = await model.find( {
'payload.timestamp': {
$gte: new Date( from ).toISOString(),
$lte: new Date( to ).toISOString()
}
} );
With the current syntax Mongoose is trying to construct a date object which is causing the Cast to date failed for value ... (type Object)
error. This syntax isn't what you'd want anyway as the database would be searching and comparing with the object which has different semantics than what you are likely looking for.
Edit to add that more details about the query semantics on this topic can be found here in the documentation.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论