Regex Mongodb中ISO日期字段的正则表达式

Regex Mongodb中ISO日期字段的正则表达式,regex,mongodb,Regex,Mongodb,如何选择时间值为00:00:00的所有日期,而不考虑日期值?正则表达式对我不起作用 { "_id" : ObjectId("59115a92bbf6401d4455eb21"), "name" : "sfdfsdfsf", "create_date" : ISODate("2013-05-13T02:34:23.000Z"), } 比如: db.myCollection.find({"create_date": /*T00:00:00.000Z/ }) 您需要首先将创

如何选择时间值为00:00:00的所有日期,而不考虑日期值?正则表达式对我不起作用

{
    "_id" : ObjectId("59115a92bbf6401d4455eb21"),
    "name" : "sfdfsdfsf",
    "create_date" : ISODate("2013-05-13T02:34:23.000Z"),
}
比如:

db.myCollection.find({"create_date": /*T00:00:00.000Z/ })

您需要首先将创建的日期转换为时间字符串,如果时间是00:00:00:000,则包含文档

db.test.aggregate([
  // Part 1: Project all fields and add timeCriteria field that contain only time(will be used to match 00:00:00:000 time)
  {
    $project: {
      _id: 1,
      name: "$name",
      create_date: "$create_date",
      timeCriteria: {
        $dateToString: {
          format: "%H:%M:%S:%L",
          date: "$create_date"
        }
      }
    }
  },
  // Part 2: match the time
  {
    $match: {
      timeCriteria: {
        $eq: "00:00:00:000"
      }
    }
  },
  // Part 3: re-project document, to exclude timeCriteria field.
  {
    $project: {
      _id: 1,
      name: "$name",
      create_date: "$create_date"
    }
  }
]);

您需要首先将创建的日期转换为时间字符串,如果时间是00:00:00:000,则包含文档

db.test.aggregate([
  // Part 1: Project all fields and add timeCriteria field that contain only time(will be used to match 00:00:00:000 time)
  {
    $project: {
      _id: 1,
      name: "$name",
      create_date: "$create_date",
      timeCriteria: {
        $dateToString: {
          format: "%H:%M:%S:%L",
          date: "$create_date"
        }
      }
    }
  },
  // Part 2: match the time
  {
    $match: {
      timeCriteria: {
        $eq: "00:00:00:000"
      }
    }
  },
  // Part 3: re-project document, to exclude timeCriteria field.
  {
    $project: {
      _id: 1,
      name: "$name",
      create_date: "$create_date"
    }
  }
]);

从MongoDB版本>=4.4,我们可以使用运算符编写自定义过滤器

注意:不要忘记根据您的要求更改时区。时区不是强制性的

让timeRegex=/.*T00:00:00.000Z$/i;
db.myCollection.find({
$expr:{
$function:{
正文:函数(createDate、timeRegex){
返回timeRegex.test(createDate);
},
args:[{$dateToString:{date:$create_date],时区:“+0530”}},timeRegex],
朗:“js”
}
}
});

从MongoDB版本>=4.4我们可以使用运算符编写自定义过滤器

注意:不要忘记根据您的要求更改时区。时区不是强制性的

让timeRegex=/.*T00:00:00.000Z$/i;
db.myCollection.find({
$expr:{
$function:{
正文:函数(createDate、timeRegex){
返回timeRegex.test(createDate);
},
args:[{$dateToString:{date:$create_date],时区:“+0530”}},timeRegex],
朗:“js”
}
}
});

告诉我投票反对的理由。我找不到stackoverflow的解决方案。提供的解决方案对我不起作用。你有什么理由拒绝投票吗?这一点对我也不起作用:不确定这是否适合你,但你可以使用aggregationMy要求找到创建时间为00:00:00(从日期和时间的组合)的所有记录并进行更新。即使对于聚合,我也需要找到一种方法来选择这些记录。Regex似乎是解决方案,但对我来说什么都不起作用。给我投票反对的理由。我找不到stackoverflow的解决方案。提供的解决方案对我不起作用。你有什么理由拒绝投票吗?这一点对我也不起作用:不确定这是否适合你,但你可以使用aggregationMy要求找到创建时间为00:00:00(从日期和时间的组合)的所有记录并进行更新。即使对于聚合,我也需要找到一种方法来选择这些记录。Regex似乎是解决方案,但对我来说什么都不管用。