pymongo - TypeError: document must be an instance of dict, bson.son.SON, or other type that inherits from collections.MutableMapping

Hydroxl picture Hydroxl · Jun 10, 2015 · Viewed 7.9k times · Source

I'm writing data to MongoDB using pymongo.

I receive this error when performing the write operation.

TypeError: document must be an instance of dict, bson.son.SON, or other type  that inherits from collections.MutableMapping

If I print the data, and copy it with an insert_one() call, the information is written with the shell. I've tried converting the variable to a str, but I am not sure how it is formatted incorrectly, as if I copy the print line and perform the operation, it is added. This leads to me to suspect that there was not a problem with the format of the JSON object, but it's encoding or some variant thereof, or small syntax of the returned JSON object.

post_id = post_db.insert_one(chunk).inserted_id

Answer

Bedi Egilmez picture Bedi Egilmez · Apr 4, 2016

I had the same error since I was passing a tuple to the insert_one() function. Double check the datatype you are passing into this function, as error explicitly says there are some datatypes are accepted to pass into this.

type(chunk) will be helpful for you.