我使用MongoDB
将数据写入pymongo
。
执行写入操作时收到此错误。
TypeError: document must be an instance of dict, bson.son.SON, or other type that inherits from collections.MutableMapping
如果我打印数据,并通过insert_one()
调用进行复制,则会使用shell编写信息。我已经尝试将变量转换为str,但我不确定它是如何被错误地格式化的,就好像我复制了打印行并执行操作一样,它被添加了。这导致我怀疑JSON对象的格式没有问题,但是它的编码或其某些变体,或者返回的JSON对象的小语法。
post_id = post_db.insert_one(chunk).inserted_id
答案 0 :(得分:4)
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.
答案 1 :(得分:-1)
当我使用.insert_one()同时插入多个对象时,我有相同的TypeError。按照教程中的建议使用批量插入(.insert_many()):http://api.mongodb.org/python/current/tutorial.html#bulk-inserts。