Last active
May 7, 2022 18:57
-
-
Save aliyevorkhan/ecb2ab5d5f4da8e170955caa88275398 to your computer and use it in GitHub Desktop.
PyMongo exercises
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pymongo | |
def connect_server(): #connect mongo server | |
myclient = pymongo.MongoClient("mongodb://localhost:27017/") | |
return myclient | |
#######################DATABASE UTILS################################# | |
def create_db(): | |
mydb = myclient["mydatabase"] #create database | |
return mydb | |
def check_db_exists(): #check database exists | |
dblist = myclient.list_database_names() | |
if "mydatabase" in dblist: | |
print("The database exists.") | |
###################################################################### | |
#######################COLLECTION UTILS############################### | |
def create_col(): #create collection | |
mycol = mydb["customers"] | |
return mycol | |
def check_col_exists(): #check collection exists | |
collist = mydb.list_collection_names() | |
if "customers" in collist: | |
print("The collection exists.") | |
###################################################################### | |
#######################INSERTION UTILS################################ | |
def insert_one(): | |
mydict = { "name": "John", "address": "Highway 37" } | |
x = mycol.insert_one(mydict) | |
print("Inserted id:", x.inserted_id) | |
def insert_many(): | |
mylist = [ | |
{ "name": "Amy", "address": "Apple st 652"}, | |
{ "name": "Hannah", "address": "Mountain 21"}, | |
{ "name": "Michael", "address": "Valley 345"}, | |
{ "name": "Sandy", "address": "Ocean blvd 2"}, | |
{ "name": "Betty", "address": "Green Grass 1"}, | |
] | |
x = mycol.insert_many(mylist) | |
#print list of the _id values of the inserted documents: | |
print(x.inserted_ids) | |
def insert_many_with_id(): | |
mylist = [ | |
{ "_id": 1, "name": "John", "address": "Highway 37"}, | |
{ "_id": 2, "name": "Peter", "address": "Lowstreet 27"}, | |
{ "_id": 3, "name": "Amy", "address": "Apple st 652"}, | |
{ "_id": 4, "name": "Hannah", "address": "Mountain 21"}, | |
{ "_id": 5, "name": "Michael", "address": "Valley 345"}, | |
] | |
x = mycol.insert_many(mylist) | |
#print list of the _id values of the inserted documents: | |
print(x.inserted_ids) | |
###################################################################### | |
########################FIND UTILS#################################### | |
def find_one(): | |
x = mycol.find_one() | |
print(x) | |
def find_all(): | |
for x in mycol.find(): | |
print(x) | |
def find_with_some_fields(): | |
for x in mycol.find({},{ "_id": 0, "name": 1, "address": 1 }): | |
print(x) | |
for x in mycol.find({},{ "address": 0 }): | |
print(x) | |
###################################################################### | |
########################QUERY UTILS################################### | |
def filter_result(): | |
myquery = { "address": "Park Lane 38" } | |
mydoc = mycol.find(myquery) | |
for x in mydoc: | |
print(x) | |
###################################################################### | |
######################SORT UTILS###################################### | |
def sort(): | |
mydoc = mycol.find().sort("name") | |
for x in mydoc: | |
print(x) | |
def sort_reverse(): | |
mydoc = mycol.find().sort("name", -1) | |
for x in mydoc: | |
print(x) | |
###################################################################### | |
########################DELETE UTILS################################## | |
def delete_one(): | |
myquery = { "address": "Mountain 21" } | |
mycol.delete_one(myquery) | |
def delete_many(): | |
myquery = { "address": {"$regex": "^S"} } | |
x = mycol.delete_many(myquery) | |
print(x.deleted_count, " documents deleted.") | |
def delete_all(): | |
x = mycol.delete_many({}) | |
print(x.deleted_count, " documents deleted.") | |
###################################################################### | |
####################DROP UTILS######################################## | |
def drop(): | |
mycol.drop() | |
###################################################################### | |
####################UPDATE UTILS###################################### | |
def update_one(): | |
myquery = { "address": "Valley 345" } | |
newvalues = { "$set": { "address": "Canyon 123" } } | |
mycol.update_one(myquery, newvalues) | |
#print "customers" after the update: | |
for x in mycol.find(): | |
print(x) | |
def update_many(): | |
myquery = { "address": { "$regex": "^S" } } | |
newvalues = { "$set": { "name": "Minnie" } } | |
x = mycol.update_many(myquery, newvalues) | |
print(x.modified_count, "documents updated.") | |
###################################################################### | |
###########################LIMIT UTILS################################ | |
def limit(): | |
myresult = mycol.find().limit(5) | |
#print the result: | |
for x in myresult: | |
print(x) | |
###################################################################### | |
if __name__ == "__main__": | |
myclient = connect_server() | |
print("DATABASE UTILS") | |
mydb=create_db() | |
check_db_exists() | |
print("COLLECTION UTILS") | |
mycol = create_col() | |
check_col_exists() | |
print("INSERT UTILS") | |
# insert_one() | |
# insert_many() | |
# insert_many_with_id() | |
print("FIND UTILS") | |
find_one() | |
find_all() | |
find_with_some_fields() | |
print("FILTER UTILS") | |
filter_result() | |
print("SORT UTILS") | |
sort() | |
sort_reverse() | |
print("DELETE UTILS") | |
# delete_one() | |
# delete_many() | |
# delete_all() | |
print("DROP UTILS") | |
# drop() | |
print("UPDATE UTILS") | |
update_one() | |
update_many() | |
print("LIMIT UTILS") | |
limit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment