0

So I'm supposed to use Codio to create a Python file that creates a new document to the existing MongoDB collection called 'inspections' in the database 'city' But it's not successfully adding the new document to the collection.

Click here for image

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()

This is how I ran the Create.py file in the Codio terminal.

Click here for image

After running the Create.py, I did db.inspections.find({"id":"0203-2019-ssp1"}) but it's not there.

Click here for image

Oluwafemi Sule
  • 36,144
  • 1
  • 56
  • 81

1 Answers1

0

pymongo.collection.Collection.save is a deprecated method in version 3.0 and up of the pymongo library 1

Ideally, you want to use the pymongo.collection.Collection.insert_one 2

def insert_document(document):
    try:
        result=collection.insert_one(document)
    except ValidationError as ve:
        abort(400, str(ve))
    return result
Oluwafemi Sule
  • 36,144
  • 1
  • 56
  • 81