title: Pymongo Find & Update problems
body: <p><strong>Background</strong>
  I'm making an application which takes an input through a Discord bot and then puts the input in a Google Document. During that process, I'm using Flask with Pymongo to find documents whose <code>isNew</code> property is set to <code>true</code> and send it back to Google Docs script. After that's done I want to set the <code>isNew</code> property of the documents to set to <code>false</code>. Alternatively, I can set the <code>isNew</code> to <code>false</code> before I send, since i can't call a function successfully, after I have returned the response in Flask.</p>
  
  <p>Based on what I've experimented with, whenever I get new documents using a <code>&lt;CollectionName&gt;.find()</code> request, and tried to update the <code>isNew</code> property subsequently using a <code>CollectionName.update_many()</code> command, it results in the variable storing the original <code>find</code> command documents to go blank. </p>
  
  <p>Code Attempts:
  1. update many while trying to return new documents</p>
  
  <pre><code>import pymongo
  
  # mongodb db connection string
  connectionString = ('mongodb://localhost:27017')
  
  client = pymongo.MongoClient(connectionString)
  db = client['tempBotDB']
  
  # 3 types of questions: quiz, cross, hunt
  # separate collections for each type of strings
  
  quizQs = db['quizQs']
  crossQs = db['crossQs']
  huntQs = db['huntQs']
  
  # Properties of a question: isnew, type, question, answer
  
  
  def saveQ(qObj):    # Saves a question to the db
  
      qType = qObj.get('type')
  
      # creating own qObj
      q = {
          'question': qObj.get('question'),
          'answer': qObj.get('answer'),
          'isNew': bool(True)
      }
  
      if qType == 'quiz':
          result = quizQs.insert_one(q)
      elif qType == 'cross':
          result = crossQs.insert_one(q)
      elif qType == 'hunt':
          result = huntQs.insert_one(q)
      else:
          result = ('Error: "type" of question can only be'
                    'one of : cross, hunt, quiz')
      return result
  
  
  def getQs(qType):   # Gets all the questions by given type
      if qType == 'quiz':
          result = quizQs.update_many(
              {'isNew': bool(True)},
              {'$set': {'isNew': bool(False)}},
              {returnOriginal: True}
           )
          print(result)
  
      else:
          result = ('Error: "type" of question can only be'
                    'one of : cross, hunt, quiz')
  
  
  getQs('quiz')
  </code></pre>
  
  <p>Error: NameError: name 'returnOriginal' is not defined
  Also tried with <code>returnNewDocument</code> instead of <code>returnOriginal</code>. But got: <code>NameError: name 'returnNewDocument' is not defined</code></p>
  
  <ol start="2">
  <li>Using a find followed by an update within the same function</li>
  </ol>
  
  <pre><code>def getQs(qType):   # Gets all the questions by given type
      if qType == 'quiz':
  
          selector = {'isNew': False}
          modifier = {'$set': {'isNew': True}}
  
          result = quizQs.find(selector)
          #fakeRes = quizQs.update_many(selector, modifier)
  
          print('res', result)
          for i in result:
              print('result', i)
          #print('fakeres', fakeRes)
          # for i in fakeRes:
          #     print('FakeRes', i)
  
      else:
          result = ('Error: "type" of question can only be'
                    'one of : cross, hunt, quiz')
  </code></pre>
  
  <p>If I uncomment the <code>update</code> command, the result of the <code>find</code> command becomes uniteratable (I get an error message), which to the best of my knowledge means that it's empty. Here's what happens:</p>
  
  <pre><code>def getQs(qType):   # Gets all the questions by given type
      if qType == 'quiz':
  
          selector = {'isNew': False}
          modifier = {'$set': {'isNew': True}}
  
          result = quizQs.find(selector)
          fakeRes = quizQs.update_many(selector, modifier)
  
          print('res', result)
          for i in result:
              print('result', i)
          print('fakeres', fakeRes)
          for i in fakeRes:
              print('FakeRes', i)
  
      else:
          result = ('Error: "type" of question can only be'
                    'one of : cross, hunt, quiz')
  </code></pre>
  
  <p>I get an the following output:</p>
  
  <pre><code>res &lt;pymongo.cursor.Cursor object at 0x10e34ca50&gt;
  fakeres &lt;pymongo.results.UpdateResult object at 0x10e35bfa0&gt;
  Traceback (most recent call last):
    File "db.py", line 63, in &lt;module&gt;
      getQs('quiz')
    File "db.py", line 55, in getQs
      for i in fakeRes:
  TypeError: 'UpdateResult' object is not iterable
  </code></pre>
  
  <p>Notice how iterating through the <code>result</code> object did not print anything, pointing to the fact that it goes empty. </p>
  
  <p>So, to conclude, my main question is: 
  How do I successfully find and update multiple documents in mongodb, while retrieving the update documents, OR just the found documents (not updated) [EITHER of them will work as i won't need to access the property I update]</p>
  
  <p>Lastly, would it be logical to first do a <code>find</code> and then <code>update_one</code> on each document while generating a list of dictionaries of the updated documents and then finally returning that list to the user?</p>
  
  <p>I'd really appreciate any help.</p>