Python
Python
Client = MongoClient("localhost:27017")
db = Client.EmployeeData
def insert():
try:
employeeID = input("Enter employee id: ")
employeeName = input("Enter employee name: ")
employeeAge = input("Enter employee age: ")
employeeCountry = input("Enter employee country: ")
db.employee.insert_one({
"id": employeeID,
"name": employeeName,
"age": employeeAge,
"country": employeeCountry
})
print("Data inserted successfully")
except Exception as e:
print("Error:", str(e))
def update():
try:
employeeID = input("Enter the employee id to update: ")
updateField = input("Enter the field to update (name/age/country):
").lower()
newValue = input(f"Enter new value for {updateField}: ")
except Exception as e:
print("Error:", str(e))
def delete():
try:
employeeID = input("Enter the employee id to delete: ")
result = db.employee.delete_one({"id": employeeID})
if result.deleted_count > 0:
print("Employee deleted successfully")
else:
print("No employee found with that ID")
except Exception as e:
print("Error:", str(e))
# New retrieve function
def retrieve():
try:
employeeID = input("Enter the employee id to retrieve: ")
employee = db.employee.find_one({"id": employeeID})
if employee:
print(f"Employee found: ID: {employee['id']}, Name: {employee['name']},
Age: {employee['age']}, Country: {employee['country']}")
else:
print("No employee found with that ID")
except Exception as e:
print("Error:", str(e))
def menu():
while True:
choice = input("\nChoose an option: \n1. Insert new employee \n2. Update
employee \n3. Delete employee \n4. Retrieve employee \n5. Exit \nEnter choice: ")
if choice == "1":
insert()
elif choice == "2":
update()
elif choice == "3":
delete()
elif choice == "4":
retrieve()
elif choice == "5":
break
else:
print("Invalid choice, please try again")
menu()