【发布时间】:2021-03-03 13:59:50
【问题描述】:
我正在尝试在 MongoDB 中根据如下日期过滤器获取所有文档:
- 仅限本周
- 仅限上周
- 仅限本月
- 仅上个月
- 自定义日期之间。
我的收藏:
[
{
_id: "123",
status: "seen",
userId: "589",
createdAt: ISODate("2020-11-17T04:35:29.646Z"),
},
{
_id: "223",
status: "seen",
userId: "589",
createdAt: ISODate("2020-11-17T04:35:29.646Z"),
},
{
_id: "474",
status: "unseen",
userId: "589",
createdAt: ISODate("2020-11-10T04:35:29.646Z"),
},
{
_id: "875",
status: "seen",
userId: "112",
createdAt: ISODate("2020-10-11T04:35:29.646Z"),
},
{
_id: "891",
status: "unseen",
userId: "112",
createdAt: ISODate("2020-10-11T04:35:29.646Z"),
},
{
_id: "891",
status: "unseen",
userId: "113",
createdAt: ISODate("2020-11-09T04:35:29.646Z"),
}]
预期结果:
- 应用 This_Week 过滤器时 - 获取 createdAt 属于本周的所有 userId,然后计算通知百分比。
[{
userId : "589",
notificationPercentage: 100% // Because userId 589 has 2 seen documents for this week.
}]
- 应用 This_Month 过滤器时:- 在本月创建了 userId 589 和 userId 113,并为其计算 notificationPercentage。
[{
userId : "589",
notificationPercentage: 66.66%
},
{
userId : "113",
notificationPercentage: 0%
}]
- 应用 Last_month 过滤器时:
[{
userId : "112",
notificationPercentage: 50% //Because only userId 112 was created in last month and finding the notification percentage for it.
}]
- 应用 Last_week 过滤器时:
[{
userId : "113",
notificationPercentage: 0% //Because only userId 113 was created in last week and finding the notification percentage for it.
},
{
userId : "589",
notificationPercentage: 0% //Because only userId 113 was created in last week and finding the notification percentage for it.
}]
通知百分比公式 -
(No of times the user has seen/no of times he got notifications) * 100
【问题讨论】:
-
查看文档。每个操作都有一个操作员
-
@minsky,我在 Mongo 文档中找不到它。如果可能的话,你可以分享它的部分的链接吗?
-
你能发布一些你拥有的示例文件
-
@sinr 抱歉回复晚了,有点忙
标签: node.js mongodb mongoose aggregation-framework