【发布时间】:2022-01-11 08:58:12
【问题描述】:
这是我来自 MongoDB 集合的数据集:
{
"_id": "60c0ace96e93993880efd337",
"lv": [
uid: 1,
"createdate": "2021-12-15T12:30:01.935Z",
"updatedAt": [
"2021-12-15T12:31:11.635Z",
"2021-12-15T12:31:51.955Z",
"2021-12-16T12:30:01.935Z",
"2021-12-16T12:30:01.935Z",
"2021-12-17T12:30:01.935Z",
"2021-12-18T12:30:01.935Z"
]
]
},
{
...
}
我只想过滤掉日期范围在 createdate 列或 updatedAt 列中的数据。
我无法得到想要的结果。不知道我在哪里犯了查询或男女同校的错误。
我尝试过的我会在这里提及。
let startA = new Date("2021-12-14");
const a = new Date(startA.setHours(startA.getHours() - 5));
const start = new Date(a.setMinutes(startA.getMinutes() - 30));
let endA = new Date("2021-12-17");
const b = new Date(endA.setHours(endA.getHours() - 5));
const end = new Date(b.setMinutes(endA.getMinutes() - 30));
const fetchData = await MyCollection.findOne(
{
_id: ObjectId(req.body.id),
'lv.createdate': { $gte: start, $lt: end },
'lv.updatedAt': {
$elemMatch: { $gte: start, $lt: end }
}
}
).lean().exec();
非常感谢任何帮助或建议。提前感谢您的互动。
【问题讨论】:
-
“我只想过滤出日期范围位于 createdate 列或 updatedAt 列中的数据。” - 但是,您的查询过滤器正在使用隐式 and(不是或)。
-
您的示例数据不是有效的 JSON,请提供有效的示例。
标签: javascript node.js mongodb