title: Which is the efficient way of updating an element in MongoDB?
body: <p>I have a collection like below</p>
  <pre><code>{
      &quot;doc_id&quot;: &quot;1234&quot;,
      &quot;items&quot;: [
          {
              &quot;item_no&quot;: 1,
              &quot;item&quot;: &quot;car&quot;,
          },
          {
              &quot;item_no&quot;: 2,
              &quot;item&quot;: &quot;bus&quot;,
          },
          {
              &quot;item_no&quot;: 3,
              &quot;item&quot;: &quot;truck&quot;,
          }
      ]
  },
  </code></pre>
  <p>I need to update an element inside items list based on a search criteria. My search criteria is, if &quot;item_no&quot; is 3, &quot;item&quot; should be updated to &quot;aeroplane&quot;.
  I have written the following two approaches in Python to solve this.</p>
  <p>Approach 1:</p>
  <pre><code>cursor = list(collection.find({&quot;doc_id&quot;: 1234}))
  for doc in cursor:
      if &quot;items&quot; in doc:
          temp = deepcopy(doc[&quot;items&quot;])
          for element in doc[&quot;items&quot;]:
              if (&quot;item_no&quot; and &quot;item&quot;) in element:
                  if element[&quot;item_no&quot;] == 3:
                      temp[temp.index(element)][&quot;item&quot;] = &quot;aeroplane&quot;
                      collection.update_one({&quot;doc_id&quot;: 1234},
                                            {&quot;$set&quot;: {&quot;items&quot;: temp}})
  </code></pre>
  <p>Approach 2:</p>
  <pre><code>cursor = list(collection.find({&quot;doc_id&quot;: 1234}))
  for doc in cursor:
      if &quot;items&quot; in doc:
         collection.find_one_and_update({&quot;doc_id&quot;: 1234}, {'$set': {'items.$[elem]': {&quot;item_no&quot;: 3, &quot;item&quot;: &quot;aeroplane&quot;}}}, array_filters=[{'elem.item_no': {&quot;$eq&quot;: 3}}])
  </code></pre>
  <p>Among the above two approaches, which one is better in terms of time complexity?</p>