Prompt
After completing the readings for this module, you will implement the fundamental operations of creating and reading documents (the C and R of CRUD) in Python. You will use the PyMongo driver to create CRUD functional access to your document collection.
- Upload the Austin Animal Center (AAC) Outcomes data set into MongoDB by importing a CSV file using the appropriate MongoDB import tool. This file is in the
/usr/local/datasets/
directory in Apporto and the filename is aac_shelter_outcomes.csv. Use the database name AAC and collection name animals. Complete the import using the mongoimport tool and take screenshots of both the import command and its execution.Note: If you completed the Module Three milestone, you have already completed this step.
- Next, you must 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
Important: Be sure to use find() instead of find_one() when developing your method.
As you develop your code, be sure to use industry standard best practices such as proper naming conventions, exception handling, and in-line comments. This will ensure that your code is easy to read and reusable for future projects.
TIP: Use the following sample code to get started. Note that the authentication to MongoDB is in the initialization method for the CRUD class.
Example Python Code to Insert a Documentfrom pymongo import MongoClientfrom 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.
- A method that inserts a document into a specified MongoDB database and collection
- Finally, create a Python testing script that imports your CRUD Python module to call and test the create and read instances of CRUD functionality. Be sure to use the username and password for the aacuser account for authentication when instantiating the class. This script should be created in a separate Jupyter Notebook IPYNB file, and should import and instantiate an object from your CRUD library to effect changes in MongoDB. After creating your script, execute it in Jupyter Notebook and take screenshots of the commands and their execution.
- Begin creating your own README file for the CRUD Python module that you began creating in the Module Four milestone. Use the README template to get started. You must address each of the following:
- Describe the purpose of the project by completing the About the Project and Motivation sections of the template.
- Demonstrate the projects functional operations by completing the Usage section. Be sure to include examples of your code and screenshots that show how your module works.
- Document the tools used, identifying each tool and including your rationale for using these tools, by completing the Installation section. Tools include any software applications as well as any libraries used to complete your work.
- Create instructions for reproducing the project by completing the Getting Started section. Discuss what the user of this CRUD Python module would need to do to get started. Some points to address are:
- Briefly describe the database and user authentication that you set up in the Module Three milestone.
- Briefly describe how you created the C and R portions of your Python module, any challenges you encountered, and how you overcame them.
Note: In this assignment, you only need to focus on creating a README for the create and read functionality of your CRUD Python module. You will continue developing the update and delete functionality as a part of Project One, which is due in Module Five. You will also need to update your README file for that assignment.
Guidelines for Submission
For your submission, you must include the code files for your Python module (PY file) and your Python testing script (IPYNB file). You must also submit a Microsoft Word document with your screenshots from Step 3.