from pymongo import MongoClient
import pprint
# Connect to MongoDB
client = MongoClient("mongodb://localhost:27017/")
db = client["BookDatabase"]
collection = db["Books"]
def add_book():
book_id =
input("Enter Book ID: ")
book_name =
input("Enter Book Name: ")
book_author =
input("Enter Book Author: ")
book_data = {
"Book_id":
book_id,
"Book_name":
book_name,
"Book_author": book_author
}
result =
collection.insert_one(book_data)
print("Book added
with ID:", result.inserted_id)
def display_books():
books = collection.find()
print("\nBooks in the
database:")
for book in books:
pprint.pprint(book)
def search_book():
book_name =
input("Enter Book Name to search: ")
query =
{"Book_name": book_name}
book =
collection.find_one(query)
if book:
pprint.pprint(book)
else:
print("Book not
found")
def sort_books():
sort_key =
input("Enter Sort Key (Book_id, Book_name, Book_author): ")
books =
collection.find().sort(sort_key)
print("\nSorted
Books:")
for book in books:
pprint.pprint(book)
No comments:
Post a Comment