Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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'

image text in transcribed

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.

image text in transcribed

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

image text in transcribed

Filetree 2. Section2 CRUD Manage Targets Create.py ermina 1 import json 2 from bson import json util PARK CS-340-UNIT from pymongo import MongoClient 4 5 6 7 8 9 10 import datetime CS-340-UNIT (master) connection = MongoClient ( ' localhost', 27017) db connection['city'] collection db['inspections' datasets .settings Create.py CRUD 11 def insert_document (document): 12 13 14 15 16 17 try: result-collection.save (document) except ValidationError as ve: abort (400, str(ve)) return result 18 def main(): 19 20 21 myDocument "id" "0203-2019-sspl" print insert_document (myDocument) main codio@section-romeo:~/workspace chmod u+x ./Create.py cospace$ dio@section-romeo:/work number212 F "_id" : ObjectId ("56d61033a378eccde8a835be"), "id": "12900-2015-CMPL", "certificate number" 5387918, "business_name""GREGORY WHITE", "date""S 18 2015", "result" "Violation Issued", "sector" "Mobile Food Vendor 881", "address" "city""BRONX", "zip" 10473, "street" "BRUCKNER B LVD", "number" 1760 { "-id" : Objectid ("56d61833a378eccde8a835b1"), "id" "12933-2015-ENFO", "certificate-number" 3828831, "business-name" U "BROOKLYN", "zip11223, "street" "AVENU : : : "BART PORK STORE OF AVENUE INC", "date" "Mar 11 2015", "result" "Pass", "sector""Grocery-Retail 808", "address""city" U", "number" 158 ype "it" for more db.inspections.find ("id": "12933-2015-ENFO") "_id" : ObjectId ("56d61033a378eccde8a835b1"), "id" "12933-2015-ENFO", "certificate number" 3020031, "business_name""BARI PORK STORE OF AVENUE U INC", "date" "Mar 11 2015", "result" "Pass", "sector" "Grocery-Retail 808", "address" "city" "BROOKLYN", "zip"11223, "street" "AVENU U", "number" 158 dio@section-romeo /workspaces chmod ux /Create.py odio@section-romeo:-/workspaces mongo ngoDB shell version: 2.6.12 nnecting to: test use city witched to db city db.inspections.find("id": "0203-2019-ssp1") Filetree 2. Section2 CRUD Manage Targets Create.py ermina 1 import json 2 from bson import json util PARK CS-340-UNIT from pymongo import MongoClient 4 5 6 7 8 9 10 import datetime CS-340-UNIT (master) connection = MongoClient ( ' localhost', 27017) db connection['city'] collection db['inspections' datasets .settings Create.py CRUD 11 def insert_document (document): 12 13 14 15 16 17 try: result-collection.save (document) except ValidationError as ve: abort (400, str(ve)) return result 18 def main(): 19 20 21 myDocument "id" "0203-2019-sspl" print insert_document (myDocument) main codio@section-romeo:~/workspace chmod u+x ./Create.py cospace$ dio@section-romeo:/work number212 F "_id" : ObjectId ("56d61033a378eccde8a835be"), "id": "12900-2015-CMPL", "certificate number" 5387918, "business_name""GREGORY WHITE", "date""S 18 2015", "result" "Violation Issued", "sector" "Mobile Food Vendor 881", "address" "city""BRONX", "zip" 10473, "street" "BRUCKNER B LVD", "number" 1760 { "-id" : Objectid ("56d61833a378eccde8a835b1"), "id" "12933-2015-ENFO", "certificate-number" 3828831, "business-name" U "BROOKLYN", "zip11223, "street" "AVENU : : : "BART PORK STORE OF AVENUE INC", "date" "Mar 11 2015", "result" "Pass", "sector""Grocery-Retail 808", "address""city" U", "number" 158 ype "it" for more db.inspections.find ("id": "12933-2015-ENFO") "_id" : ObjectId ("56d61033a378eccde8a835b1"), "id" "12933-2015-ENFO", "certificate number" 3020031, "business_name""BARI PORK STORE OF AVENUE U INC", "date" "Mar 11 2015", "result" "Pass", "sector" "Grocery-Retail 808", "address" "city" "BROOKLYN", "zip"11223, "street" "AVENU U", "number" 158 dio@section-romeo /workspaces chmod ux /Create.py odio@section-romeo:-/workspaces mongo ngoDB shell version: 2.6.12 nnecting to: test use city witched to db city db.inspections.find("id": "0203-2019-ssp1")

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

MongoDB Applied Design Patterns Practical Use Cases With The Leading NoSQL Database

Authors: Rick Copeland

1st Edition

1449340040, 978-1449340049

More Books

Students also viewed these Databases questions