Mongoose 查询查找同一文档中日期不匹配的文档
我正在尝试编写一个猫鼬查询,它可以找到 REVIEW_DATE
在过去 24 小时内的所有文档,并且 actions 数组中没有与 TYPE
匹配的操作,DETAIL_TYPE
和 ACTION_DATE
与 REVIEW_DATE
不匹配。基本上,如果 REVIEW_DATE 发生变化,我需要重新执行操作,因此审核日期的值会记录在 actionDetails 中。
{
reviewDate: <REVIEW_DATE>,
actions: [
{
actionType: <TYPE>,
actionDetails: {
detailType: <DETAIL_TYPE>,
valueOfReviewDateAtTimeOfAction: <ACTION_DATE>
}
}
]
}
由于 mongo 不支持自联接,因此我将撤回 REVIEW_DATE 在过去 24 小时内的所有文档,然后通过迭代操作列表来迭代结果过滤列表。有没有办法通过单个查询或聚合来做到这一点?
I am trying to write a mongoose query that can find all documents where REVIEW_DATE
is within the last 24 hours and there is no action in the actions array that matches TYPE
, DETAIL_TYPE
, and ACTION_DATE
does not match REVIEW_DATE
. Basically if REVIEW_DATE changes, I need to re-perform the action, so the value of the review date is recorded in the actionDetails.
{
reviewDate: <REVIEW_DATE>,
actions: [
{
actionType: <TYPE>,
actionDetails: {
detailType: <DETAIL_TYPE>,
valueOfReviewDateAtTimeOfAction: <ACTION_DATE>
}
}
]
}
Because mongo doesn't support a self join, I am pulling back all documents where REVIEW_DATE is within the last 24 hours and then iterating through the results filtering the list by iterating over the list of actions. Is there a way do do this with a single query or aggregation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我正确地回答了您的问题,我相信您需要生成一个使用
$expr
运算符,因为它允许您在同一文档中使用聚合管道表达式,例如$dateDiff
- 获取两个日期之间的差异,并使用 a 确定
是否在过去 24 小时内系统聚合变量$$NOW
作为当前日期时间值的引用$filter
- 过滤actions
数组以仅返回那些与给定条件匹配的元素$size
- 返回的长度过滤的actions
数组$gt
/$lt
- 返回一个用于$expr
条件来判断过滤后的数组是否有在同一文档上满足上述查询条件的元素。例如,您可以尝试运行以下查询:
或者对于不支持
$dateDiff
If I got your question correctly, I believe that you need to generate a query that uses the
$expr
operator as it allows you to use aggregate pipeline expressions within the same document such as$dateDiff
- get the difference between two dates and determine wether the<REVIEW_DATE>
is within the last 24 hours using a system aggregation variable$$NOW
as reference to the current datetime value$filter
- filter theactions
array to return only those elements that match the given conditions$size
- returns the length of the filteredactions
array$gt
/$lt
- returns a bool that is used for the$expr
condition to determine whether the filtered array has elements or not that satisfy the above query criteria on the same document.For example, you could try running the following query:
or for MongoDB versions which do not have support for
$dateDiff