Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
database
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ajil.k
database
Commits
4f38c2d0
Commit
4f38c2d0
authored
Feb 06, 2023
by
ajil.k
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
updated mongo_db functions
parent
8cd28115
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
211 additions
and
121 deletions
+211
-121
scripts/core/handlers/delete_mongodb.py
+0
-22
scripts/core/handlers/get_data_mongodb.py
+31
-0
scripts/core/handlers/insert_mongodb.py
+0
-35
scripts/core/handlers/mongodb_functions.py
+160
-0
scripts/core/handlers/read_documents_mongodb.py
+0
-14
scripts/core/handlers/update_mongodb.py
+0
-23
scripts/core/handlers/update_postgresql.py
+3
-2
scripts/db_connection/mongodb_connect.py
+2
-2
scripts/db_connection/postgreSQL_connect.py
+1
-1
scripts/services/crud_operations.py
+14
-22
No files found.
scripts/core/handlers/delete_mongodb.py
deleted
100644 → 0
View file @
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
)
scripts/core/handlers/get_data_mongodb.py
View file @
4f38c2d0
...
...
@@ -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
)
scripts/core/handlers/insert_mongodb.py
deleted
100644 → 0
View file @
8cd28115
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
)
scripts/core/handlers/mongodb_functions.py
0 → 100644
View file @
4f38c2d0
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
)
scripts/core/handlers/read_documents_mongodb.py
deleted
100644 → 0
View file @
8cd28115
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
)
scripts/core/handlers/update_mongodb.py
deleted
100644 → 0
View file @
8cd28115
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
)
scripts/core/handlers/update_postgresql.py
View file @
4f38c2d0
...
...
@@ -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
=
in
put
(
"Enter the data: "
)
update_data
=
in
t
(
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
()
...
...
scripts/db_connection/mongodb_connect.py
View file @
4f38c2d0
#
i
mporting pymongo
#
I
mporting pymongo
import
pymongo
def
db_connect
():
#
c
reate a connection
#
C
reate a connection
try
:
connection
=
pymongo
.
MongoClient
(
"mongodb://localhost:27017/"
)
print
(
"Connected Successfully !"
)
...
...
scripts/db_connection/postgreSQL_connect.py
View file @
4f38c2d0
...
...
@@ -7,7 +7,7 @@ def connect_db():
password
=
"test"
,
host
=
"127.0.0.1"
,
port
=
"5432"
,
database
=
"comp
any
_db"
)
database
=
"comp_db"
)
return
connection
except
(
Exception
,
psycopg2
.
Error
)
as
error
:
...
...
scripts/services/crud_operations.py
View file @
4f38c2d0
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
(
)
#
find
ing more than one document
inserting_many
(
company_collection
)
#
insert
ing more than one document
InsertionMongoDB
()
.
inserting_many
(
)
# finding one document
finding_one
(
company_collection
)
ReadMongoDB
()
.
finding_one
(
)
#
insert
ing more than one document
finding_many
(
company_collection
)
#
find
ing 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
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment