所以我应该使用 Codio 创建一个 Python 文件,该文件在数据库“city”中为现有的 MongoDB 集合创建一个名为“inspections”的新文档,但它没有成功地将新文档添加到集合中。
import json
from bson import json_util
from pymongo import MongoClient
connection = MongoClient('localhost', 27017)
db = connection['city']
collection = db['inspections']
def insert_document(document):
try:
result=collection.save(document)
except ValidationError as ve:
abort(400, str(ve))
return result
def main():
print "Hi"
myDocument = { "id" : "0203-2019-ssp1",
"certificate_number" : 5382334,
"business_name" : "BUILDING TO SERVE INC.",
"date" : "Jul 22 2015",
"result" : "Violation Issued",
"sector" : "Home Improvement Contractor - 100",
"address" : [ "city" : "JAMAICA", "zip" : 11432, "street" :
"HILLSIDE AVE", "number" : 17939 ] }
print insert_document(myDocument)
main()
这就是我在 Codio 终端中运行 Create.py 文件的方式。
运行 Create.py 后,我做了 db.inspections.find({"id":"0203-2019-ssp1"}) 但它不存在。