1 回答
TA贡献1876条经验 获得超5个赞
我没有看到任何write_concern被传入作为您写入的选项,我假设您可能看不到WriteConcernError. 检查此:pymongo.write_concern.WriteConcern有关如何设置的示例WriteConcern。此外,仅当您想在发生某种类型的错误时执行某些功能时才需要这些错误检查,因为您只需要返回True/ False,那么您可以删除所有这些错误检查:
代码 :
connection = MongoClient('localhost', 27017)
db = connection['city']
collection = db['inspections']
def insert_document(documentToInsert):
try:
collection.insert_one(documentToInsert)
return True
except Exception as e:
print("An exception occurred ::", e)
return False
def main():
document = {
"id": "11111-2019-ENFO",
"certificate_number": 9278806,
"business_name": "TAXOLOGY",
"date": "Feb 20 2015",
"result": "No Violation Issued",
"sector": "Accounting - 111",
"address":
{
"city": "MISSION HILLS",
"zip": 91401,
"street": "Sepulveda",
"number": 1809
}
}
print(insert_document(document))
main()
添加回答
举报