[英]check date existence on array of string
这是我来自 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();
非常感谢任何帮助或建议。 提前感谢您的互动。
您的输入数据无效。 有了一些假设,解决方案可能是这个:
db.MyCollection.aggregate([
{
$set: {
lv: {
$map: {
input: "$lv",
as: "lv",
in: {
$filter: {
input: "$$lv.updatedAt",
cond: {
$and: [
{ $gte: ["$$this", start] },
{ $lt: ["$$this", end] }
]
}
}
}
}
}
}
}
])
或者这个:
db.MyCollection.aggregate([
{
$set: {
"lv.updatedAt": {
$filter: {
input: "$lv.updatedAt",
cond: {
$and: [
{ $gte: ["$$this", start] },
{ $lt: ["$$this", end] }
]
}
}
}
}
}
])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.