【问题标题】:how do I export a datetime field in string format with mongo export?如何使用 mongo 导出以字符串格式导出日期时间字段?
【发布时间】:2018-02-23 21:01:30
【问题描述】:

我正在尝试使用 mongoexport 从 mongo 导出整个集合。我发现的大多数关于日期的答案都涉及查询。我不做查询。我将整个表转储为 JSON 格式。日期时间字段是这样导出的:

"dateOfBirth": {
  "$date": "1999-02-02T00:00:00.000Z"
}

我只想要日期时间字段的字符串表示,所以它看起来像:

"dateOfBirth": "1999-02-02T00:00:00.000Z"

我不知道 mongo 的确切版本,但它是最近的版本。

我该怎么做?

【问题讨论】:

    标签: mongodb export mongoexport


    【解决方案1】:

    可以通过使用 dateToString 运算符来获得这种格式。因此,在您的情况下,仅日期部分将是

     "dateOfBirth" :  { $dateToString: { format: "%Y-%m-%d", date: 
     "$dateOfBirth" } } 
    

    根据您的需要检查时间格式herehere的可能性

    【讨论】:

      【解决方案2】:

      这是一个 Python 脚本,它将 mongo 不友好的日期格式转换为导出的 json 转储中的字符串格式:

      
      ######################################
      # About                              #
      ######################################
      
      # Author:   Eric Arnol-Martin https://eamster.tk
      # Purpose:  Replaces Mongo's Exported DateTime Format with Proper DateTime String Representations for Easier Import for Other Databases / Programming Languages
      # Expects:  Mongo JSON Exported Pretty File.  
      #           For example, a file produced by a command similar to 
      #           "mongoexport -d {db_name} --host localhost:27017 -c {table_name} --jsonArray --pretty -o {table_name}.json"
      # Tests:    RegEx Test Link:  https://regexr.com/68l7r
      # Outputs:  Creates a copy of the input JSON file with DateTime objects replaced with their proper string representation in the same directory as the original file 
      #           with the same file name suffixed with "_NEW" at the end of it.
      # Sources:  https://stackoverflow.com/questions/2503413/regular-expression-to-stop-at-first-match
      #           https://stackoverflow.com/questions/159118/how-do-i-match-any-character-across-multiple-lines-in-a-regular-expression
      
      ######################################
      # Imports                            #
      ######################################
      
      import re
      from os.path import exists
      import fileinput
      
      
      ######################################
      # Actual Program                     #
      ######################################
      
      path = input ("Enter path or name of file to parse: ")
      prevPiece = None
      content_new = ""
      boolReplaceFromNextLine = False
      boolHandlingLong = False
      
      if exists(path):
          # Clear new file
          b = open(path + "_NEW", "w+")
          b.close()
          count = 0
          recordCount = 0
      
          for line in fileinput.input(files=path):
              count = count + 1
              if '"$date":' in line: 
                  content_new = content_new[0:content_new.rindex('{')] + line.replace('"$date": {', '').replace('"$date":', '').replace('\n', '').strip();
                  boolReplaceFromNextLine = True
              else:
                  if boolReplaceFromNextLine:
                      if '"$numberLong":' in line:
                          content_new = content_new + line.replace('"$numberLong":', '').replace('\n', '').strip();
                          boolReplaceFromNextLine = True
                          boolHandlingLong = True
                      else:
                          if boolHandlingLong:
                              content_new = content_new + line.replace('}', '').strip();
                              boolReplaceFromNextLine = True
                              boolHandlingLong = False
                          else:
                              boolReplaceFromNextLine = False
                              content_new = content_new + line.replace('}', '').strip() + '\n';
                  else:
                      content_new = content_new + line        
              
              if line == '},\n' and content_new:
                  recordCount = recordCount + 1
                  b = open(path + "_NEW", "a+")
                  b.write(content_new)
                  b.close()   
                  content_new = ""
                  print('Record ' + str(recordCount) + ' processed... adding it to the file...')
                  
              prevPiece = line
          
          if content_new:
              b = open(path + "_NEW", "a+")
              b.write(content_new)
              b.close()   
              content_new = ""
              recordCount = recordCount + 1
              print('Record ' + str(recordCount) + ' processed... adding it to the file...')
      
      

      根据需要进行调整。这适用于 14GB 导出的 json 文件。我们需要这个从 mongo 到 SQL Server 的转换。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-01-08
        • 2018-10-28
        • 2019-12-14
        • 1970-01-01
        • 2014-08-10
        • 1970-01-01
        • 2015-07-23
        • 1970-01-01
        相关资源
        最近更新 更多