title: How to speed up this PyMongo query/loop
body: <p>I am using the Riot API for League of Legends to make pulls in Python. I have one collection that is only IDs for matchmade games that I have not pulled from the API and another that is the pulled matchmade games. Altogether I currently have 94,040 pulled matches and 224,346 match IDs that I have not pulled yet.</p>
  
  <p>In my code I am checking out profiles of people for new match IDs to add against the matches I already have before adding it to the collection of IDs I haven't pulled yet. It is taking around 7 seconds for each iteration through the second collection.find, and I am not sure why. I have both of the collections indexed, and when I was using collection.find({}).sort({}) it was actually taking longer for some reason.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>import pymongo
  import time
  
  
  client = pymongo.MongoClient('mongodb://localhost')
  match_db = client.matched_games
  summoner_db = client.summoner_match_history
  match_id_db = client.match_id
  
  matches = match_db.matches
  summoners = summoner_db.summoners
  match_id = match_id_db.match
  
  
  def main():
      for matchid in summoners.find({}, {'matches.gameId': 1, '_id': 0}, no_cursor_timeout=True):
          timer = time.time()
          try:
              for match in matchid['matches']:
                  print(match)
                  match = match['gameId']
                  for k in matches.find({}):
                      k = k['gameId']
                      if k == match:
                          print(time.time() - timer)
                          timer = time.time()
                          print('Already have match #' + str(k))
                          break
                  else:
                      print(time.time() - timer)
                      timer = time.time()
                      print('Inserting match #' + str(match) + '.')
                      match_id.insert({'match_id': match})
          except KeyError as e:
              print(e)
              pass
          except pymongo.errors.DuplicateKeyError as e:
              # print(e)
              pass
  
  if __name__ == '__main__':
      main()
  </code></pre>