我正在使用 pymongo
将数据写入 MongoDB
。
我在执行写入操作时收到此错误。
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
最佳答案
自从我将元组传递给 insert_one()
函数以来,我遇到了同样的错误。仔细检查您传递到此函数的数据类型,因为错误明确表示接受某些数据类型传递到此函数。
type(chunk)
会对你有帮助。
关于mongodb - pymongo - TypeError : document must be an instance of dict, bson.son.SON,或从collections.MutableMapping继承的其他类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30766880/