title: Cleansing Data with Updates - Mongodb + Python
body: <p>I have imported the into Mongodb but not able to cleanse the data in Python. Please see the below question and the script. I need answer of Script 1 &amp; 2</p>
  
  <hr>
  
  <ul>
  <li><p>import it into MongoDB, cleanse the data in Python, and update MongoDB with the cleaned data. Specifically, you'll be taking a people dataset where some of the birthday fields look like this:</p>
  
  <p><code>{
      ...
      "birthday": ISODate("2011-03-17T11:21:36Z"),
      ...
   }</code></p></li>
  <li><p>And other birthday fields look like this:</p>
  
  <p><code>{
       ...
       "birthday": "Thursday, March 17, 2011 at 7:21:36 AM",
       ...
   }</code></p></li>
  <li><p>MongoDB natively supports a Date datatype through BSON. This datatype is used in the first example, but a plain string is used in the second example. In this assessment, you'll complete the attached notebook to script a fix that makes all of the document's birthday field a Date.</p></li>
  <li><p>Download the notebook and dataset to your notebook directory. Once you have the notebook up and running, and after you've updated your connection URI in the third cell, continue through the cells until you reach the fifth cell, where you'll import the dataset. This can take up to 10 minutes depending on the speed of your Internet connection and computing power of your computer.</p></li>
  <li><p>After verifying that all of the documents have successfully been inserted into your cluster, you'll write a query in the 7th cell to find all of the documents that use a string for the birthday field.</p></li>
  <li><p>To verify your understanding of the first part of this assessment, how many documents had a string value for the birthday field (the output of cell 8)?</p></li>
  </ul>
  
  <h2>Script1</h2>
  
  <ul>
  <li><p>Replace YYYY with a query on the people-raw collection that will return a cursor with only</p></li>
  <li><p>documents where the birthday field is a string</p></li>
  <li><p>people_with_string_birthdays = YYYY</p></li>
  <li><p>This is the answer to verify you completed the lab: </p>
  
  <p><code>people_with_string_birthdays.count()</code></p></li>
  </ul>
  
  <h2>Script2</h2>
  
  <pre><code>updates = []
  # Again, we're updating several thousand documents, so this will take a little while
  for person in people_with_string_birthdays:
      # Pymongo converts datetime objects into BSON Dates. The dateparser.parse function 
      # returns a datetime object, so we can simply do the following to update the field
      # properly. Replace ZZZZ with the correct update operator
      updates.append(UpdateOne(
          {"_id": person["_id"]}, 
          {ZZZZ: { "birthday": dateparser.parse(person["birthday"]) } }
      ))
      count += 1
  
      if count == batch_size:
          people_raw.bulk_write(updates)
          updates = []
  
      count = 0
  
  if updates:         
      people_raw.bulk_write(updates)
      count = 0
  
  # If everything went well this should be zero
  people_with_string_birthdays.count()
  </code></pre>