pymongo错误文档必须是dict的实例

2024-10-02 10:26:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图将对象放入集合中,但收到的错误是:

raise TypeError("%s must be an instance of dict, bson.son.SON, "
TypeError: document must be an instance of dict, bson.son.SON, bson.raw_bson.RawBSONDocument, or a type that inherits from collections.MutableMapping

我的代码如下所示:

class DataBase:

    def __init__(self):
        self.myclient = pymongo.MongoClient("mongodb://localhost:27018/")
        self.mydb = self.myclient["MMA_TOURNAMNET"]
        self.tournamnets = self.mydb["tournamnets"]

    def insert_new_tournamnet(self, tournament):
        jason_tour = json.dumps(tournament.__dict__)
        print(jason_tour)
        self.tournamnets.insert_one(jason_tour)

我调用这个类的函数,比如:self.data_base.insert_new_tournamnet(tour)

转换后的巡更对象的形式为:

{"list_fighters": [{"Name": "Barry Boday", "Test Status": 0, "Weeks in Bubble": 0}, {"Name": "Steve Vaughn", "Test Status": 0, "Weeks in Bubble": 0}, {"Name": "Jack Zink", "Test Status": 1, "Weeks in Bubble": 0}], "schedule_list": [[{"Name": "Barry Boday", "Test Status": 0, "Weeks in Bubble": 0}, {"Name": "Steve Vaughn", "Test Status": 0, "Weeks in Bubble": 0}]]}

将Json格式放入集合中似乎很好。但我无法摆脱这个错误。有什么想法吗


Tags: 对象nameintestselfstatus错误dict

热门问题