title: Updating a MongoDB document with PyMongo
body: <p>I am trying to update a document in a collection:</p>
  
  <pre><code>   for bookerName,bookingId in zip(bookerNames,bookingIds):
          Person.update_one({'Name': bookerName}, {'$push': {'Bookings': bookingId}})
  </code></pre>
  
  <p>I am trying to add a new bookingId to the list of Bookings this Person already has.</p>
  
  <p>I receive the following error:</p>
  
  <pre><code>bson.errors.InvalidDocument: Cannot encode object: 0
  </code></pre>
  
  <p>From what I understand about this error, it means Mongo don't know how to encode a list, it treats it as a custom type. </p>
  
  <p>However, this is strange since I can do:</p>
  
  <pre><code>for bookerName in BookerNames:
      personRecord = {"Name":bookerName,"Bookings":[1,2,3,4]}
      Person.insert_one(personRecord)
  </code></pre>
  
  <p>And it works perfectly.</p>
  
  <p>I have also tried to update using $set and even by deleting the old record and trying to insert a new one with the updated list.</p>
  
  <p>Any idea why this is happening or how can I update a list in a document?</p>
  
  <p>I am using Python 3.5, PyMongo 3.0.3 and MongoDB 3.0.7. Could the difference in version between PyMongo and MondoDB be the issue?</p>
  
  <p>EDIT:</p>
  
  <pre><code>for bookerName in bookerNames:
      bookingList = bookersToBookings[bookerName]
      personRecord = {"Name":bookerName,"Bookings":bookingList}
      Person.insert_one(personRecord)
  </code></pre>
  
  <p>Where bookersToBookings is a dictionary mapping bookers to their bookingIds, as list.</p>
  
  <p>This does not work, although if I make a record with a list like [1,2,3], it works.</p>
  
  <p>EDIT 2:</p>
  
  <pre><code>for bookerName in bookerNames:
      personRecord = {"Name":bookerName,"Bookings":[]}
      Person.insert_one(personRecord)
  for x in Person.find():
      print(x)
  </code></pre>
  
  <p>This works. It prints:</p>
  
  <pre><code>{'Name': 'Alice', '_id': ObjectId('56241ad79e44c71641efa383'), 'Bookings': []}
  {'Name': 'John', '_id': ObjectId('56241ad79e44c71641efa384'), 'Bookings': []}
  {'Name': 'Jane', '_id': ObjectId('56241ad79e44c71641efa385'), 'Bookings': []}
  {'Name': 'Mary', '_id': ObjectId('56241ad79e44c71641efa386'), 'Bookings': []}
  {'Name': 'Dan', '_id': ObjectId('56241ad79e44c71641efa387'), 'Bookings': []}
  </code></pre>
  
  <p>This also works:</p>
  
  <pre><code>for bookerName in bookerNames:
      personRecord = {"Name":bookerName,"Bookings":[1,2,3]}
      Person.insert_one(personRecord)
  </code></pre>
  
  <p>Result:</p>
  
  <pre><code>{'Bookings': [1, 2, 3], '_id': ObjectId('56241b029e44c716497b60b2'), 'Name': 'Alice'}
  {'Bookings': [1, 2, 3], '_id': ObjectId('56241b029e44c716497b60b3'), 'Name': 'John'}
  {'Bookings': [1, 2, 3], '_id': ObjectId('56241b029e44c716497b60b4'), 'Name': 'Jane'}
  {'Bookings': [1, 2, 3], '_id': ObjectId('56241b029e44c716497b60b5'), 'Name': 'Mary'}
  {'Bookings': [1, 2, 3], '_id': ObjectId('56241b029e44c716497b60b6'), 'Name': 'Dan'}
  </code></pre>
  
  <p>Even this works:</p>
  
  <pre><code>for bookerName in bookerNames:
      y = [1,2,3,4]
      personRecord = {"Name":bookerName,"Bookings":[x for x in y]}
      Person.insert_one(personRecord)
  </code></pre>
  
  <p>Result:</p>
  
  <pre><code>{'_id': ObjectId('56241b5a9e44c7165395a0f0'), 'Bookings': [1, 2, 3, 4], 'Name': 'Alice'}
  {'_id': ObjectId('56241b5a9e44c7165395a0f1'), 'Bookings': [1, 2, 3, 4], 'Name': 'John'}
  {'_id': ObjectId('56241b5a9e44c7165395a0f2'), 'Bookings': [1, 2, 3, 4], 'Name': 'Jane'}
  {'_id': ObjectId('56241b5a9e44c7165395a0f3'), 'Bookings': [1, 2, 3, 4], 'Name': 'Mary'}
  {'_id': ObjectId('56241b5a9e44c7165395a0f4'), 'Bookings': [1, 2, 3, 4], 'Name': 'Dan'}
  </code></pre>
  
  <p>This, however, does not work:</p>
  
  <pre><code>for bookerName in bookerNames:
      y = bookersToBookings[bookerName]
      personRecord = {"Name":bookerName,"Bookings":[x for x in y]}
      Person.insert_one(personRecord)
  </code></pre>
  
  <p>bookersToBookings looks like this:</p>
  
  <pre><code>{'Mary': [5, 9], 'Jane': [4, 7, 8, 13, 14], 'Alice': [0, 6, 10], 'Dan': [12], 'John': [1, 2, 3, 11]}
  </code></pre>
  
  <p>EDIT 3 - SOLVED:</p>
  
  <p>For $%#^ sake! The list was made of numpy64 ints, not python ints. It works now.</p>
  
  <p>Thanks for the help!</p>