简体   繁体   English

在 MongoDB 中查找和更改现有的日期字段格式

[英]Find and change existing date field formats in MongoDB

I've looked into several similar questions but none seem to be what I need, aside from looking complicated.我已经研究了几个类似的问题,但除了看起来很复杂之外,似乎没有一个是我需要的。 Here's the scenario: I have a messy collection with multiple "date" fields, with names I already know (eg "dateTime", "FirstDate", "Status.date"...).这是场景:我有一个包含多个“日期”字段的凌乱集合,其中的名称我已经知道(例如“dateTime”、“FirstDate”、“Status.date”......)。

For each of them, I would like to:对于他们每个人,我想:

  1. Query those which are not in 'dd/mm/YYYY' or 'YYYY-mm-dd' format (for instance there might be fields with random numbers as date such as '534432' or '0', or empty values or even strings) and save their _id (or copying, in any file format; MongoExport does not work)查询那些不是'dd/mm/YYYY' 或 'YYYY-mm-dd' 格式的字段(例如可能有带有随机数字的字段,例如 '534432' 或 '0' 或空值甚至是字符串)并保存他们的_id (或以任何文件格式复制;MongoExport 不起作用)
  2. Change the ones in 'dd/mm/YYYY' and 'YYYY-mm-dd' to ISODate format将 'dd/mm/YYYY' 和 'YYYY-mm-dd' 中的更改为 ISODate 格式

The company I work in unfortunately uses a free edition of NoSQLBooster, in Version 6.2.13.不幸的是,我工作的公司使用 NoSQLBooster 的免费版本,版本为 6.2.13。 From what I could see, it has many limited capabilities, so please keep that in mind.据我所知,它有许多有限的功能,所以请记住这一点。 Is it possible to do this?是否有可能做到这一点? Any help will be much appreciated!任何帮助都感激不尽!

You can do the following steps:您可以执行以下步骤:

  1. use $ifNull to choose the date field from your list of known field names使用$ifNull从已知字段名称列表中选择日期字段
  2. try both types of date parsing('dd/mm/YYYY' or 'YYYY-mm-dd') on the chosen field using $dateFromString使用$dateFromString在所选字段上尝试两种类型的日期解析('dd/mm/YYYY' 或 'YYYY-mm-dd')
  3. create a sanitized field to store successful parsing result if any of them succeeded;如果其中任何一个成功,则创建一个已清理的字段以存储成功的解析结果; just save null if both parsing failed如果两个解析都失败,则保存 null
  4. use $out to output the final result to some collection for your future processing使用$out将最终结果输出到某个集合以供您将来处理

Here is the Mongo playground for your reference.这是Mongo游乐场供您参考。

Sharing my approach for posterity, I have come across a similar situation, where I need to update my collection's date format from String to date for the known fields, I have done the below approach for the update分享我的后代方法,我遇到了类似的情况,我需要将我的集合的日期格式从字符串更新为已知字段的日期,我已经完成了以下更新方法

For instance, I have date fields such as datefield1 and datefield2 in String format which I need to change into date format例如,我有字符串格式的datefield1datefield2等日期字段,我需要将其更改为日期格式

db.getCollection('collection_name').updateMany({ $or: [{ "datefield1": { '$type': "string"} },
 { "datefield2": { '$type': "string"} }] }
      , [
    { "$addFields": {
        "datefield1": {
            "$toDate": "$datefield1"
        }
    } },
    { "$addFields": {
        "datefield2": {
            "$toDate": "$datefield2"
        }
    } }
])

In the above case, my existing date string was a proper ISO format "2020-12-03T10:06:53.502Z" , hence I used the $toDate , in case of custom/ different date format follow the below approach在上述情况下,我现有的日期字符串是正确的 ISO 格式"2020-12-03T10:06:53.502Z" ,因此我使用了$toDate ,如果是自定义/不同的日期格式,请遵循以下方法


db.getCollection('collection_name').updateMany({ $or: [{ "datefield1": { '$type': "string"} }, 
{ "datefield2": { '$type': "string"} }] }
      , [
    { "$addFields": {
        "datefield1": { 
            "$dateFromString": { 
                "dateString": "$datefield1",
                "format": "%Y-%m-%dT%H:%M:%S.%LZ" 
            } 
        }
    } 
    },  { "$addFields": {
        "datefield2": { 
            "$dateFromString": { 
                "dateString": "$datefield2",
                "format": "%Y-%m-%dT%H:%M:%S.%LZ" 
            } 
        }
    } 
    }
])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM