[英]Find all n days documents in MongoDB
I've been looking to get data for the last n days.我一直在寻找过去 n 天的数据。 For example, I want to just all the data of the last 3 days.例如,我只想获取最近 3 天的所有数据。 After implementing a solution from another question in StackOverflow I'm not getting all the documents.在 StackOverflow 中实施另一个问题的解决方案后,我没有得到所有文档。 I am only getting one document.我只得到一份文件。 If I want to see the documents for the last 3 days it is only showing data for one particular day.如果我想查看过去 3 天的文件,它只会显示某一天的数据。
Here's my Schema:这是我的架构:
dayWiseClicks: [
{
date: {
type: Date,
},
dailyClicks: {
type: Number,
default: 0,
},
},
],
Here's the data I have before performing the query:这是我在执行查询之前拥有的数据:
{
"_id": {
"$oid": "61eff4bacf8335c7013f8065"
},
"dayWiseClicks": [
{
"dailyClicks": 3,
"_id": {
"$oid": "61eff5db5dca56cae4530db2"
},
"date": {
"$date": "2022-01-24T18:00:00.000Z"
}
},
{
"dailyClicks": 4,
"_id": {
"$oid": "61eff60b5dca56cae4530db6"
},
"date": {
"$date": "2022-01-25T18:00:00.000Z"
}
},
{
"dailyClicks": 2,
"_id": {
"$oid": "61eff64a5dca56cae4530dba"
},
"date": {
"$date": "2022-01-26T18:00:00.000Z"
}
},
{
"dailyClicks": 7,
"_id": {
"$oid": "61f60ce51f14b01f8f5be936"
},
"date": {
"$date": "2022-01-29T18:00:00.000Z"
}
},
{
"dailyClicks": 11,
"_id": {
"$oid": "61f7b1d3931b0f8bc33703d4"
},
"date": {
"$date": "2022-01-30T18:00:00.000Z"
}
},
{
"dailyClicks": 8,
"_id": {
"$oid": "61f8bdf63cc3a51b72474cb9"
},
"date": {
"$date": "2022-01-31T18:00:00.000Z"
}
},
{
"dailyClicks": 7,
"_id": {
"$oid": "61fba7159692624ce8ea04d6"
},
"date": {
"$date": "2022-02-02T18:00:00.000Z"
}
}
],
}
In theory, if I want to see the last 3 days of data.理论上,如果我想查看最近 3 天的数据。 It should be showing data of 31st Jan, 2nd February but It is only showing Data of 31st January.它应该显示 1 月 31 日、2 月 2 日的数据,但它只显示 1 月 31 日的数据。
Here's the data I am getting:这是我得到的数据:
{
"message": "Url By ID",
"result": {
"_id": "61eff4bacf8335c7013f8065",
"dayWiseClicks": [
{
"dailyClicks": 8,
"_id": "61f8bdf63cc3a51b72474cb9",
"date": "2022-01-31T18:00:00.000Z"
}
]
}
}
Here's my Code:这是我的代码:
exports.lastNDays = async (req, res) => {
try {
const url = await URL.findById(
{ _id: req.params.id },
{
dayWiseClicks: {
$elemMatch: {
date: {
$gte: moment().add(-3, "days"),
},
},
},
}
)
return res.status(200).json({
message: "Url By ID",
result: url,
});
} catch (error) {
return res.status(404).json({ error: error.message });
}
};
Can any one tell me exactly where I am making the mistake?谁能告诉我我在哪里犯了错误?
The aggregation->$filter option seems more suitable for the task, example: aggregation->$filter 选项似乎更适合该任务,例如:
db.collection.aggregate([{
$match: {
"_id": {
"$oid": "61eff4bacf8335c7013f8065"
}
}
},
{
$project: {
dayWiseClicks: {
$filter: {
input: "$dayWiseClicks",
as: "item",
cond: {
$gte: [
"$$item.date",
{
"$date": "2022-01-30T18:00:00.000Z"
}
]
}
}
}
}
}
])
Explained:解释:
If your dayWiseClicks
are in natural date-ascending order (and it seems that way), then to generically capture the last 3 days of any sequence of dates can be done with $slice
:如果您的dayWiseClicks
是自然的日期升序排列(并且看起来是这样),那么通常可以使用$slice
来捕获任何日期序列的最后 3 天:
db.foo.aggregate([
{$addFields: {dayWiseClicks: {$slice: ["$dayWiseClicks", -3]}}}
]);
You can prepend $match
stages as detailed in previous answers.您可以在前面的答案中详细说明$match
阶段。
NOTE: Coming up in v5.4 (available now in 5.2 rapid release for MongoDB Atlas) is the long-awaited sortArray
operator.注意:即将在 v5.4 中出现(现在在 MongoDB Atlas 的 5.2 快速版本中可用)是期待已久的sortArray
运算符。 If the dayWiseClicks
was not in date order, then making it that way and finding the last 3 dates is freshingly simple:如果dayWiseClicks
没有按日期顺序排列,那么这样做并找到最后 3 个日期非常简单:
db.foo.aggregate([
{$addFields: {dayWiseClicks: {$slice: [{$sortArray:{input: "$dayWiseClicks", sortBy: {"date":1}}, -3]}}}
]);
Similarly, to get the last 3 dates but in descending order:同样,要按降序获取最后 3 个日期:
db.foo.aggregate([
{$addFields: {dayWiseClicks: {$slice: [{$sortArray:{input: "$dayWiseClicks", sortBy: {"date":-1}}, 3]}}}
]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.