【发布时间】:2017-03-29 19:53:20
【问题描述】:
需求:我想要一个集合所有did not perform the "View" event in last 5 hours的客户。
数据:
{
name: "Sheldon",
events: [
{
event: "View",
timestamp: "timestamp equivalent to 10 hours ago"
},
{
event: "Some other event",
timestamp: "timestamp equivalent to 8 hours ago"
},
{
event: "View",
timestamp: "timestamp equivalent to 2 hours ago"
}
]
},
{
name: "Leonard",
events: [
{
event: "View",
timestamp: "timestamp equivalent to 10 hours ago"
}
]
},
{
name: "Howard",
events: [
{
event: "View",
timestamp: "timestamp equivalent to 2 hours ago"
}
]
},
{
name: "Raj",
events: [
{
event: "Some other event",
timestamp: "timestamp equivalent to 6 hours ago"
}
]
}
我尝试了以下方法,但它总是返回 "Sheldon"(可能是因为其他事件最终满足了条件?)。
q.where({
$and: [
{
'events': {
$not: {
$elemMatch: {
event: "View",
timestamp: {
$gte: "timestamp equivalent to 5 hours ago"
}
}
}
}
}
]
});
我该怎么做才能只返回 "Leonard" 和 "Raj" 的文档?
【问题讨论】:
-
检查一次this question。
-
您可以查看 (docs.mongodb.com/manual/reference/operator/query-comparison) 了解更多详情。
-
@TanmayBaranwal 我的示例查询等同于您链接中的查询,但无法按预期工作,因为还有另一个满足条件的事件。
-
你有 ISO 格式的日期吗?为什么不用实际日期更新您的帖子?这将使尝试回答和测试您的代码的人们变得容易。