我试图将对象放入集合中,但收到的错误是:
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格式放入集合中似乎很好。但我无法摆脱这个错误。有什么想法吗
只需将您的记录保留为dict并将其传递给
insert_one()
:相关问题 更多 >
编程相关推荐