Question
I need help, Not sure what im doing wrong. I keep getting this error: True --------------------------------------------------------------------------- TypeError Traceback (most recent call last) in 26 locateRec.locate(critter)
I need help, Not sure what im doing wrong. I keep getting this error:
True
--------------------------------------------------------------------------- TypeError Traceback (most recent call last)in 26 locateRec.locate(critter) 27 ---> 28 updateRec.update(changeCritter) 29 30 locateRec.locate(critter) /usr/local/datasets/animalsCRUD.py in update(self, record) 48 if record is not None: 49 ---> 50 update_result = self.database.animals.find_one_and_update(record) 51 return update_result.modified_count 52 else: TypeError: find_one_and_update() missing 1 required positional argument: 'update'
Here is my test code:
import pymongo from pymongo import ReturnDocument import datetime from animalsCRUD import AnimalShelter
#username = "aacuser" #password = "password"
insertRec = AnimalShelter("aacuser", "password") locateRec = AnimalShelter("aacuser", "password") updateRec = AnimalShelter("aacuser", "password") deleteRec = AnimalShelter("aacuser", "password")
animal = ({"age_upon_outcome":"5 years", "animal_id":"A333333", "animal_type":"Dog", "breed":"Derp", "color":"White", "date_of_birth":"07/19/19", "datetime": datetime.datetime.now(), "name":"", "outcome_subtype":"Foster", "outcome_type":"Adoption", "sex_upon_outcome":"Intact Female", "location_lat":30.60784677, "location_long":-97.35087807, "age_upon_outcome_in_weeks":64.24642857})
critter = {"animal_id":"A333333"}
changeCritter = ({"animal_id": "A333333"}, {'$set': {'animal_type': 'Cat'}})
print(insertRec.create(animal))
locateRec.locate(critter)
updateRec.update(changeCritter)
locateRec.locate(critter)
deleteRec.delete(critter)
And here is my python code:
from pymongo import MongoClient from bson.objectid import ObjectId import datetime
class 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:33638/?authMechanism=DEFAULT&authSource=AAC' % ("aacuser", "password")) self.database = self.client['AAC']
# Method to implement the C in CRUD. def create(self, data):
#Verify that dictionary containing record data was provided, else raise an exception if data is not None: #Store the results of the insert to variable insert_result = self.database.animals.insert_one(data) # data should be dictionary
#If insert was successful, return True, else, return False if insert_result.inserted_id: status = True else: status = False return status else: raise Exception("Nothing to save, because data parameter is empty")
# Method to implement the R in CRUD. def locate(self,record):
#Verify that search criteria was provided, else raise an exception if record is not None:
#Search animals collection in the AAC database & print cursor location search_result = self.database.animals.find(record) print(search_result) else: raise Exception("No search criteria provided")
# Method to implement the U in CRUD def update(self, record):
#Verify that the record exists; if so, update accordingly if record is not None: update_result = self.database.animals.find_one_and_update(record) return update_result.modified_count else: raise Exception("Record not found")
# Method to implement the D in CRUD def delete(self, record):
#Verify that the record to be deleted has been supplied if record is not None:
delete_result = self.database.animals.delete_one(data) else: raise Exception("No record provided.")
Step by Step Solution
There are 3 Steps involved in it
Step: 1
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