0% found this document useful (0 votes)
3 views

Python

Uploaded by

shaikhsadaf0892
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Python

Uploaded by

shaikhsadaf0892
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Step 1 : open code - see path - copy - file manager - paste code - enter- script -

cmd - pip install pymongo

step 2 : write code in idle and run

step 3: open mongo an cross check

from pymongo import MongoClient

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}: ")

if updateField in ["name", "age", "country"]:


db.employee.update_one(
{"id": employeeID},
{"$set": {updateField: newValue}}
)
print(f"Employee {updateField} updated successfully")
else:
print("Invalid field selection!")

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()

You might also like