[Python] Pymongo bson.errors.InvalidDocument: Cannot encode Object when read from file

Discussão em 'Python' iniciado por Stack, Maio 4, 2025.

  1. Stack

    Stack Membro Participativo

    I have a csv file with a header row and 100k entries in it.

    I am using python in pycharm IDE and conda as python interpreter. conda suppies pymongo-3.4.0 as mongo-driver.

    I parse my csv file and save it as jsonn file on disk, and then iterate over the json file to insert the json objects in the mongo database.

    JSON looks like this:

    {
    a1 : {'name': 702104534, type='Single' }
    a2 : {'name': 702104535, type='Single' }
    a3 : {'name': 702104536, type='Married' }
    }


    Python Code:

    import pymongo
    import pickle

    with open('my_json_file.json', 'rb') as in_file:
    person_json = pickle.load(in_file)

    conn = pymongo.MonogClient('localhost','27017')
    db = conn['db']
    coll = db['coll']

    persons = person_json.keys()
    for person in persons:
    coll.insert_one(person_json[person])


    Throughs the following error:

    Traceback (most recent call last):
    File "C:/PyCharmWorkSpace/LCR/json2mongo.py", line 109, in <module>
    data2mongo()
    File "C:/PyCharmWorkSpace/LCR/json2mongo.py", line 89, in data2mongo
    coll.insert_one(json_str)
    File "C:\Anaconda3\lib\site-packages\pymongo\collection.py", line 657, in insert_one
    bypass_doc_val=bypass_document_validation),
    File "C:\Anaconda3\lib\site-packages\pymongo\collection.py", line 562, in _insert
    check_keys, manipulate, write_concern, op_id, bypass_doc_val)
    File "C:\Anaconda3\lib\site-packages\pymongo\collection.py", line 543, in _insert_one
    check_keys=check_keys)
    File "C:\Anaconda3\lib\site-packages\pymongo\pool.py", line 424, in command
    self._raise_connection_failure(error)
    File "C:\Anaconda3\lib\site-packages\pymongo\pool.py", line 552, in _raise_connection_failure
    raise error
    File "C:\Anaconda3\lib\site-packages\pymongo\pool.py", line 419, in command
    collation=collation)
    File "C:\Anaconda3\lib\site-packages\pymongo\network.py", line 94, in command
    None, codec_options, check_keys)
    bson.errors.InvalidDocument: Cannot encode object: 702104534


    However, same works, if the json string is a variable

    Python Code:

    import pymongo

    conn = pymongo.MonogClient('localhost','27017')
    db = conn['db']
    coll = db['coll']

    person = {'name': 702104534, 'type'='Single' }
    coll.insert_one(person)


    Thanks for your help.

    Continue reading...

Compartilhe esta Página