Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Develop a Python module in a PY file, using object-oriented programming methodology, to enable create and read functionality for the database. To support code reusability,
Develop a Python module in a PY file, using object-oriented programming methodology, to enable create and read functionality for the database. To support code reusability, your Python code needs to be importable as a module by other Python scripts.
Develop a CRUD class that, when instantiated, provides the following functionality:
- A method that inserts a document into a specified MongoDB database and collection
- Input -> argument to function will be set of key/value pairs in the data type acceptable to the MongoDB driver insert API call
- Return -> “True” if successful insert, else “False”
- A method that queries for documents from a specified MongoDB database and specified collection
- Input -> arguments to function should be the key/value lookup pair to use with the MongoDB driver find API call
- Return -> result in cursor if successful, else MongoDB returned error message
Can someone help clarify how to create a class that inserts and queries documents in mongoDB?
Example Python Code to Insert a Document from pymongo import MongoClient from bson.objectid import ObjectIdclass AnimalShelter(object): """ CRUD operations for Animal collection in MongoDB """ def __init__(self, username, password): # Initializing the MongoClient. This helps to # access the MongoDB databases and collections. self.client = MongoClient('mongodb://%s:%s@localhost:27017' % (username, password)) self.database = self.client['project']# Complete this create method to implement the C in CRUD. def create(self, data): if data is not None: self.database.animals.insert(data) # data should be dictionary else: raise Exception("Nothing to save, because data parameter is empty")# Create method to implement the R in CRUD.
Step by Step Solution
★★★★★
3.54 Rating (168 Votes )
There are 3 Steps involved in it
Step: 1
it is the example python module that uses objectoriented programming methodology to provide create and read functionality for a MongoDB database impor...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started