Commit 4f38c2d0 by ajil.k

updated mongo_db functions

parent 8cd28115
def delete_one_document(company_collection):
try:
# delete one document
deleted_one = company_collection.delete_one({"company_name": "hjdf"})
if deleted_one.acknowledged:
print("One document deleted")
else:
print("Document deletion failed!")
except Exception as e:
print("Error ", e)
def delete_many_document(company_collection):
try:
# delete more than one document
deleted_many = company_collection.delete_many({"pat_change": 33.3})
if deleted_many.acknowledged:
print("Documents deleted successfully")
else:
print("Documents deletion failed!")
except Exception as e:
print("Error ", e)
......@@ -12,3 +12,34 @@ def data_from_user():
"pat_change": pat_change
}
return company_document
def get_update_data():
try:
column = None
update_data = None
print("Enter the field which you want to update.\n"
"1.company_name\n"
"2.market_cap\n"
"3.revenue_change\n"
"4.pat_change\n")
choice = int(input("Enter the choice: "))
if choice == 1:
column = "company_name"
update_data = input("Enter the Company name: ")
#updated_one = self.collection.update_one({'company_license_no': license_no},
# {"$set": {"company_name": update_data}})
elif choice == 2:
column = "market_cap"
update_data = int(input("Enter the market cap: "))
elif choice == 3:
column = "revenue_change"
update_data = float(input("Enter the revenue_change: "))
elif choice == 4:
column = "pat_change"
update_data = float(input("Enter the pat_change: "))
else:
get_update_data()
return column, update_data
except Exception as e:
print("Exception due to ", e)
from scripts.core.handlers.get_data_mongodb import data_from_user
def inserting_one(company_collection):
try:
# insert one document
print("------Inserting using insert_one------")
# get data from user
data = data_from_user()
# inserting one document
inserted_one = company_collection.insert_one(data)
if inserted_one.acknowledged:
print("One document inserted successfully!")
else:
print("One document insertion failed!")
except Exception as e:
print("Error ", e)
def inserting_many(company_collection):
try:
# insert more than one document
print("------Inserting using insert_many------")
number_of_documents = int(input("Enter the number of documents to be inserted: "))
document_list = []
for i in range(0, number_of_documents):
print("----- Enter the details of company " + str(i + 1) + " -----\n")
document_list.append(data_from_user())
# inserting more than one document
inserted_many = company_collection.insert_many(document_list)
if inserted_many.acknowledged:
print(str(number_of_documents) + " documents inserted successfully!")
except Exception as e:
print("Error ", e)
from scripts.core.handlers.get_data_mongodb import data_from_user
from scripts.db_connection.mongodb_connect import db_connect
class OperationMongoDB:
def __init__(self):
self.db = db_connect()
collection_name = "company"
self.collection = self.db[collection_name]
class InsertionMongoDB(OperationMongoDB):
def inserting_one(self):
try:
# insert one document
print("------Inserting data using insert_one------")
# get data from user
data = data_from_user()
# inserting one document
inserted_one = self.collection.insert_one(data)
if inserted_one.acknowledged:
print("One document inserted successfully!")
else:
print("One document insertion failed!")
except Exception as e:
print("Error ", e)
def inserting_many(self):
try:
# insert more than one document
print("------Inserting using insert_many------")
number_of_documents = int(input("Enter the number of documents to be inserted: "))
document_list = []
for i in range(0, number_of_documents):
print("----- Enter the details of company " + str(i + 1) + " -----\n")
document_list.append(data_from_user())
# inserting more than one document
inserted_many = self.collection.insert_many(document_list)
if inserted_many.acknowledged:
print(str(number_of_documents) + " documents inserted successfully!")
except Exception as e:
print("Error ", e)
class ReadMongoDB(OperationMongoDB):
def finding_many(self):
try:
# find one document
print("Display all documents using find\n")
print(list(self.collection.find({}, {"company_name": 1, "_id": 0, "company_license_no": 1})))
except Exception as e:
print("Error ", e)
def finding_one(self):
try:
# find one document
print("Finding one document\n")
license_no = int(input("Enter the company license no to find the document: "))
print(self.collection.find_one({"company_license_no": license_no}, {"_id": 0}))
except Exception as e:
print("Error ", e)
class UpdateMongoDB(OperationMongoDB):
def update_one_document(self):
try:
# update one
license_no = int(input("Enter the company license no to update the document: "))
print("Enter the field which you want to update.\n"
"1.company_name\n"
"2.market_cap\n"
"3.revenue_change\n"
"4.pat_change\n")
choice = int(input("Enter the choice: "))
if choice == 1:
update_data = input("Enter the Company name: ")
updated_one = self.collection.update_one({'company_license_no': license_no},
{"$set": {"company_name": update_data}})
elif choice == 2:
update_data = int(input("Enter the market cap: "))
updated_one = self.collection.update_one({'company_license_no': license_no},
{"$set": {"market_cap": update_data}})
elif choice == 3:
update_data = float(input("Enter the revenue_change: "))
updated_one = self.collection.update_one({'company_license_no': license_no},
{"$set": {"revenue_change": update_data}})
elif choice == 4:
update_data = float(input("Enter the pat_change: "))
updated_one = self.collection.update_one({'company_license_no': license_no},
{"$set": {"pat_change": update_data}})
else:
updated_one = 0
print("Error occurred due to incorrect entry")
if updated_one.matched_count == 1:
print("One document updated!")
else:
print("Updating one document failed!")
except Exception as e:
print("Error ", e)
def update_many_document(self):
try:
# update many
print("Add a new column in the document")
column = input("Enter the column name: ")
value = input("Enter the value: ")
updated_many = self.collection.update_many({}, {"$set": {column: value}})
if updated_many.matched_count != 0:
print("Documents updated with new column successfully!")
else:
print("Updating failed!")
except Exception as e:
print("Error ", e)
class DeleteMongoDB(OperationMongoDB):
def delete_one_document(self):
try:
# delete one document
print("Delete one document\n")
license_no = int(input("Enter the company license no the document to be deleted: "))
deleted_one = self.collection.delete_one({"company_license_no": license_no})
if deleted_one.acknowledged:
print("One document deleted")
else:
print("Document deletion failed!")
except Exception as e:
print("Error ", e)
def delete_many_document(self):
try:
# delete more than one document
print("Delete more than one documents\n")
print("Enter the field based which you want to delete.\n"
"1.company_name\n"
"2.market_cap\n"
"3.revenue_change\n"
"4.pat_change\n")
choice = int(input("Enter the choice: "))
if choice == 1:
value = input("Enter the company name: ")
deleted_many = self.collection.delete_many({"company_name": value})
elif choice == 2:
value = int(input("Enter the market cap: "))
deleted_many = self.collection.delete_many({"market_cap": value})
elif choice == 3:
value = float(input("Enter the revenue change: "))
deleted_many = self.collection.delete_many({"revenue_change": value})
elif choice == 4:
value = float(input("Enter the pat change: "))
deleted_many = self.collection.delete_many({"pat_change": value})
else:
print("Error invalid input")
if deleted_many.acknowledged:
print("Documents deleted successfully")
else:
print("Documents deletion failed!")
except Exception as e:
print("Error ", e)
def finding_one(company_collection):
try:
# find one document
print(list(company_collection.find({}, {"company_name": 1, "_id": 0, "company_license_no": 1})))
except Exception as e:
print("Error ", e)
def finding_many(company_collection):
try:
# find one document
print(company_collection.find_one())
except Exception as e:
print("Error ", e)
def update_one_document(company_collection):
try:
# update one
updated_one = company_collection.update_one({'company_name': "Reliance Industries Ltd."},
{"$set": {"pat_change": 33.4}})
if updated_one.matched_count == 1:
print("One document updated!")
else:
print("Updating one document failed!")
except Exception as e:
print("Error ", e)
def update_many_document(company_collection):
try:
# update many
updated_many = company_collection.update_many({}, {"$set": {"pat_change": 3.6}})
if updated_many.matched_count != 0:
print("Documents updated Successfully!")
else:
print("Updating documents failed!")
except Exception as e:
print("Error ", e)
......@@ -10,8 +10,9 @@ def update_row(connection):
"4.pat_change\n")
field_choice = int(input("Enter your choice: "))
if field_choice == 1:
field_to_update = 'company_name'
update_data = input("Enter the data: ")
update_query = f'update tbl_company SET company_name ={update_data} where license_no={license_no}'
update_query = f'update tbl_company SET {field_to_update}={update_data} WHERE license_no={license_no}'
else:
if field_choice == 2:
field_to_update = "market_cap"
......@@ -19,7 +20,7 @@ def update_row(connection):
field_to_update = "revenue_change"
elif field_choice == 4:
field_to_update = "pat_change"
update_data = input("Enter the data: ")
update_data = int(input("Enter the data: "))
update_query = f'UPDATE tbl_company SET {field_to_update}={update_data} WHERE license_no={license_no}'
cursor.execute(update_query)
connection.commit()
......
# importing pymongo
# Importing pymongo
import pymongo
def db_connect():
# create a connection
# Create a connection
try:
connection = pymongo.MongoClient("mongodb://localhost:27017/")
print("Connected Successfully !")
......
......@@ -7,7 +7,7 @@ def connect_db():
password="test",
host="127.0.0.1",
port="5432",
database="company_db")
database="comp_db")
return connection
except (Exception, psycopg2.Error) as error:
......
from scripts.db_connection.mongodb_connect import db_connect
from scripts.db_connection.postgreSQL_connect import connect_db
from scripts.core.handlers.insert_mongodb import inserting_one, inserting_many
from scripts.core.handlers.read_documents_mongodb import finding_one, finding_many
from scripts.core.handlers.delete_mongodb import delete_one_document, delete_many_document
from scripts.core.handlers.update_mongodb import update_one_document, update_many_document
# Importing libraries
from scripts.core.handlers.delete_postgresql import delete_row
from scripts.core.handlers.insert_postgresql import insert
from scripts.core.handlers.mongodb_functions import InsertionMongoDB, ReadMongoDB, UpdateMongoDB, DeleteMongoDB
from scripts.core.handlers.read_data_postgresql import read_data
from scripts.core.handlers.delete_postgresql import delete_row
from scripts.core.handlers.update_postgresql import update_row
from scripts.db_connection.postgreSQL_connect import connect_db
def start_crud_operation_mongodb():
try:
# create a db connection
db = db_connect()
# create a collection
company_collection = db['company']
# inserting one document
inserting_one(company_collection)
InsertionMongoDB().inserting_one()
# finding more than one document
inserting_many(company_collection)
# inserting more than one document
InsertionMongoDB().inserting_many()
# finding one document
finding_one(company_collection)
ReadMongoDB().finding_one()
# inserting more than one document
finding_many(company_collection)
# finding more than one document
ReadMongoDB().finding_many()
# updating one document
update_one_document(company_collection)
UpdateMongoDB().update_one_document()
# updating many documents
update_many_document(company_collection)
UpdateMongoDB().update_many_document()
# deleting one document
delete_one_document(company_collection)
DeleteMongoDB().delete_one_document()
# deleting many documents
delete_many_document(company_collection)
DeleteMongoDB().delete_many_document()
except Exception as e:
print("Error-", e)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment