【发布时间】:2021-01-03 14:56:35
【问题描述】:
我对 MongoDB 有点陌生,但在查询它时遇到了问题。
假设我有以下数据集,
[
{
_id: '1',
date: "2020-12-31T22:02:11.257Z",
},
{
_id: '2',
date: "2020-12-31T22:05:11.257Z",
},
{
_id: '3',
date: "2021-01-01T22:02:11.257Z",
},
{
_id: '4',
date: "2021-01-02T12:02:11.257Z",
},
{
_id: '5',
date: "2021-01-02T22:02:11.257Z",
}
]
我正在尝试按天对所有记录进行分组。从我的前端,我发送了一个多月,然后我根据它运行查询。因此,如果用户选择一月,我将运行以下查询:
router.get('/', async (req, res) => {
const {selectedMonth, selectedYear} = req.query; // january would be '1' here
const data = await db.collection.find({"date": {
"$gt": new Date(selectedYear, parseInt(selectedMonth) - 1, 1),
"$lte": new Date(selectedYear, parseInt(selectedMonth), 1)
}}).sort({ date: -1 })
在这里,我正在获取所选范围内的所有记录。因此,如果用户选择 2021 年 1 月,我将获取大于 2020 年 12 月 31 日且小于或等于 2021 年 1 月 31 日的所有记录。
这里的问题是我想统计每天的所有记录。我能够获取指定日期范围内的所有记录,但我正在寻找类似下面的内容,以便返回:
[
"2021-01-01": [
{ _id: '3', date: "2021-01-01T22:02:11.257Z" },
],
"2021-01-02": [
{ _id: '4', date: "2021-01-02T12:02:11.257Z" },
{ _id: '5', date: "2021-01-02T22:02:11.257Z" },
]
]
我正在考虑遍历返回的数据并构建我自己的响应对象,但我想知道是否有更好的方法来做到这一点?这是我目前正在做的事情,
const result = []
let count = 0;
data.forEach((record, index) => {
// first record will always set the base
if (index === 0) {
result.push({
date: record.date.toLocaleDateString(),
count: 1
})
} else {
// If the record is the same date, then increase counter
if (record.date.toLocaleDateString() === result[count].date) {
result[count].count = result[count].count + 1
} else {
// push a new record and increase count
result.push({
date: record.date.toLocaleDateString(),
count: 1
})
count = count + 1
}
}
});
产量,
result [
{ date: '1/2/2021', count: 2 },
{ date: '1/1/2021', count: 1 }
]
【问题讨论】:
标签: javascript mongodb mongoose