title: Pymongo delete documents after a certain date
body: <p>I have a python script that I want to delete some MongoDB documents when the <code>published</code> key date is over 30 days.</p>
  
  <p>My code currently is:</p>
  
  <pre><code>def db_rotate(mongo_server, mongo_port):
      try:
          logging.info('Connecting to MongoDB')
          client = MongoClient(mongo_server, mongo_port)
          db = client['vuln_sets']
          logging.info('Connected to MongoDB')
          today = datetime.now()
          last_month = today - timedelta(days=30)
          result = db.vulnerabilities.delete_many({'Published': last_month})
          logging.info('Deleted ' + str(result.deleted_count) + ' vulnerabilities')
      except Exception as e:
      logging.exception(e)
  </code></pre>
  
  <p>This code runs with no errors but does not remove any documents and I believe this to be because the date in the <code>Published</code> key has the date in Unix format i.e. <code>2017-10-30T11:36:20</code> whereas the last_month variable uses a different format.</p>
  
  <p>The code which inserts the documents is:</p>
  
  <pre><code>import json
  import logging
  import logging.handlers
  import os
  import pymongo
  from datetime import timedelta, datetime
  from pymongo import MongoClient
  
  
  def import_json(mongo_server,mongo_port, vuln_folder):
      try:
          logging.info('Connecting to MongoDB')
          client = MongoClient(mongo_server, mongo_port)
          db = client['vuln_sets']
          coll = db['vulnerabilities']
          logging.info('Connected to MongoDB')
          basepath = os.path.dirname(__file__)
          filepath = os.path.abspath(os.path.join(basepath, ".."))
          archive_filepath = filepath + vuln_folder
          filedir = os.chdir(archive_filepath)
          file_count = 0
          for item in os.listdir(filedir):
              if item.endswith('.json'):
                  file_name = os.path.abspath(item)
                  with open(item, 'r') as currentfile:
                      vuln_counter = 0
                      duplicate_count = 0
                      logging.info('Currently processing ' + item)
                      file_count +=1
                      json_data = currentfile.read()
                      vuln_content = json.loads(json_data)
                      today = datetime.now()
                      last_month = today - timedelta(days=30)
                      for vuln in vuln_content:
                          try:
                              del vuln['_type']
                              new_vuln = {key: vuln[key] for key in vuln if key != '_source'}
                              new_vuln.update(vuln['_source'])
                              if new_vuln['published'] &gt;= str(last_month):
                                  coll.insert(new_vuln, continue_on_error=True)
                                  vuln_counter += 1
                              else:
                                  pass
                          except pymongo.errors.DuplicateKeyError:
                              duplicate_count +=1
  
                  logging.info('Added ' + str(vuln_counter) + ' vulnerabilities for ' + item)
                  logging.info('Found ' + str(duplicate_count) + ' duplicate records!')
                  os.remove(file_name)
          logging.info('Processed ' + str(file_count) + ' files')
      except Exception as e:
          logging.exception(e)
  </code></pre>
  
  <p>An example of the documents inserted is:</p>
  
  <pre><code>{
      "_id" : "CESA-2017:3081",
      "_index" : "bulletins",
      "_score" : null,
      "sort" : [
          103042
      ],
      "lastseen" : "2017-10-30T20:42:09",
      "references" : [
          "https://access.redhat.com/errata/RHSA-2017:3081"
      ],
      "affectedPackage" : [
  
      ],
      "description" : "",
      "edition" : 1,
      "reporter" : "",
      "published" : "2017-10-30T11:36:20",
      "title" : "",
      "type" : "centos",
      "enchantments" : {
  
      },
      "bulletinFamily" : "unix",
      "cvelist" : [
          "",
      ],
      "modified" : "2017-10-30T11:36:20",
      "id" : "CESA-2017:3081",
      "href" : "http://lists.centos.org/pipermail/centos-announce/2017-October/022611.html",
      "cvss" : {
          "score" : 6.8,
          "vector" : "AV:NETWORK/AC:MEDIUM/Au:NONE/C:PARTIAL/I:PARTIAL/A:PARTIAL/"
      }
  }
  </code></pre>