Uploaded image for project: 'Python Driver'
  1. Python Driver
  2. PYTHON-1664

Unable to serialize object in the insert_one query

    • Type: Icon: New Feature New Feature
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 3.8
    • Affects Version/s: 3.7, 3.7.2
    • Component/s: Query Operations
    • None
    • Environment:
      Jupyter and Python

      Hello,

      It's a very strange error. The code is:

      import pandas as pd
      import datetime as dt
      import pymongo as mdb

      dB = mdb.MongoClient(host='T121015PC038')
      collection = dB.dev.test

      data = {'date':[dt.datetime.today()], 'integer': [10], 'string':['test'], 'double':[1.49]}
      df = pd.DataFrame(data=data)

      row = df.loc[0].to_dict()
      collection.insert_one({'date':dt.datetime(2018,10,26), 'items':[row]})

       

      Mainly, the error is generated because the integer value is encoded like a numpy.int64.

       

      How can I fix it?

       

      the traceback is:

      --------------------------------------------------------------------------- InvalidDocument Traceback (most recent call last) <ipython-input-5-cb351e9e5640> in <module>() 1 row = df.loc[0].to_dict() ----> 2 collection.insert_one(

      {'date':dt.datetime(2018,10,26), 'items':[row]}

      ) C:\ProgramData\Anaconda3\lib\site-packages\pymongo\collection.py in insert_one(self, document, bypass_document_validation, session) 691 write_concern=write_concern, 692 bypass_doc_val=bypass_document_validation, --> 693 session=session), 694 write_concern.acknowledged) 695 C:\ProgramData\Anaconda3\lib\site-packages\pymongo\collection.py in insert(self, docs, ordered, check_keys, manipulate, write_concern, op_id, bypass_doc_val, session) 605 return self._insert_one( 606 docs, ordered, check_keys, manipulate, write_concern, op_id, --> 607 bypass_doc_val, session) 608 609 ids = [] C:\ProgramData\Anaconda3\lib\site-packages\pymongo\collection.py in _insert_one(self, doc, ordered, check_keys, manipulate, write_concern, op_id, bypass_doc_val, session) 593 594 self.database.client._retryable_write( --> 595 acknowledged, _insert_command, session) 596 597 if not isinstance(doc, RawBSONDocument): C:\ProgramData\Anaconda3\lib\site-packages\pymongo\mongo_client.py in _retryable_write(self, retryable, func, session) 1246 """Internal retryable write helper.""" 1247 with self._tmp_session(session) as s: -> 1248 return self._retry_with_session(retryable, func, s, None) 1249 1250 def __reset_server(self, address): C:\ProgramData\Anaconda3\lib\site-packages\pymongo\mongo_client.py in _retry_with_session(self, retryable, func, session, bulk) 1199 raise last_error 1200 retryable = False -> 1201 return func(session, sock_info, retryable) 1202 except ServerSelectionTimeoutError: 1203 if is_retrying(): C:\ProgramData\Anaconda3\lib\site-packages\pymongo\collection.py in _insert_command(session, sock_info, retryable_write) 588 session=session, 589 client=self.database.client, --> 590 retryable_write=retryable_write) 591 592 _check_write_command_response(result) C:\ProgramData\Anaconda3\lib\site-packages\pymongo\pool.py in command(self, dbname, spec, slave_ok, read_preference, codec_options, check, allowable_errors, check_keys, read_concern, write_concern, parse_write_concern_error, collation, session, client, retryable_write, publish_events) 582 # Catch socket.error, KeyboardInterrupt, etc. and close ourselves. 583 except BaseException as error: --> 584 self._raise_connection_failure(error) 585 586 def send_message(self, message, max_doc_size): C:\ProgramData\Anaconda3\lib\site-packages\pymongo\pool.py in _raise_connection_failure(self, error) 743 _raise_connection_failure(self.address, error) 744 else: --> 745 raise error 746 747 def __eq_(self, other): C:\ProgramData\Anaconda3\lib\site-packages\pymongo\pool.py in command(self, dbname, spec, slave_ok, read_preference, codec_options, check, allowable_errors, check_keys, read_concern, write_concern, parse_write_concern_error, collation, session, client, retryable_write, publish_events) 577 compression_ctx=self.compression_context, 578 use_op_msg=self.op_msg_enabled, --> 579 unacknowledged=unacknowledged) 580 except OperationFailure: 581 raise C:\ProgramData\Anaconda3\lib\site-packages\pymongo\network.py in command(sock, dbname, spec, slave_ok, is_mongos, read_preference, codec_options, session, client, check, allowable_errors, address, check_keys, listeners, max_bson_size, read_concern, parse_write_concern_error, collation, compression_ctx, use_op_msg, unacknowledged) 112 request_id, msg, size, max_doc_size = message._op_msg( 113 flags, spec, dbname, read_preference, slave_ok, check_keys, --> 114 codec_options, ctx=compression_ctx) 115 # If this is an unacknowledged write then make sure the encoded doc(s) 116 # are small enough, otherwise rely on the server to return an error. C:\ProgramData\Anaconda3\lib\site-packages\pymongo\message.py in _op_msg(flags, command, dbname, read_preference, slave_ok, check_keys, opts, ctx) 677 flags, command, identifier, docs, check_keys, opts, ctx) 678 return _op_msg_uncompressed( --> 679 flags, command, identifier, docs, check_keys, opts) 680 finally: 681 # Add the field back to the command. InvalidDocument: Cannot encode object: 10

            Assignee:
            shane.harvey@mongodb.com Shane Harvey
            Reporter:
            nos86 Salvatore Musumeci
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: