From b4e2c9c29cb776d835d6f20f6feca80c96750bf8 Mon Sep 17 00:00:00 2001 From: theng Date: Wed, 3 Nov 2021 01:30:47 +0800 Subject: [PATCH 1/3] Draft --- app.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app.py b/app.py index 4028883..04b685a 100644 --- a/app.py +++ b/app.py @@ -1,6 +1,11 @@ # Create your own db.py with mongodb connection details, DO NOT commit that file # PM me on telegram your connection string, username and password from db import db +URL = "URL_LINK".format( + DB_USERNAME, DB_PWD) +print(URL) +client = pymongo.MongoClient(URL) +db = client["RHDEVS-BE-Mongo"] # Create From 2c8be399c6bd18f2adb988ca16448b93b847496f Mon Sep 17 00:00:00 2001 From: theng Date: Fri, 31 Dec 2021 17:24:01 +0800 Subject: [PATCH 2/3] Update .gitignore --- .gitignore | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index ce514ff..460c0fc 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -db.py \ No newline at end of file +db.py +__pycache__ +*.pyc \ No newline at end of file From 55481923201a302679f3e3a1d52b7859273b6912 Mon Sep 17 00:00:00 2001 From: theng Date: Fri, 31 Dec 2021 17:24:30 +0800 Subject: [PATCH 3/3] Add required CRUD operations --- app.py | 43 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/app.py b/app.py index 04b685a..d3151c6 100644 --- a/app.py +++ b/app.py @@ -1,16 +1,53 @@ # Create your own db.py with mongodb connection details, DO NOT commit that file # PM me on telegram your connection string, username and password from db import db -URL = "URL_LINK".format( - DB_USERNAME, DB_PWD) -print(URL) +from db import DB_PASSWORD, DB_USERNAME, URL_LINK +import pymongo +from bson.objectid import ObjectId +import datetime + +URL = URL_LINK.format(DB_USERNAME, DB_PASSWORD) client = pymongo.MongoClient(URL) db = client["RHDEVS-BE-Mongo"] # Create +shop = db.shop.find_one({"shopName": "Al Amaans"}) +user = db.users.find_one({"userID": 1}) + +order = {} +order["customer"] = {"userID": user["userID"], "name": user["name"], "address": user["address"]} +order["shop"] = {"shopID": shop["shopID"], "shopName": shop["shopName"]} +order["amount"] = 10.00 +order["status"] = "order successful" +order["orderDate"] = datetime.datetime.utcnow() + +# returnedResult1 = db.orders.insert_one(order) +# print(returnedResult1.inserted_id) + # Read +cursor = db.orders.find().sort("amounts", pymongo.DESCENDING) + +print(cursor[4:9]) + # Update +targetDate = datetime.datetime(2021,12,31,0,0,0,0) +filter_condition = {"$and": [ + {"status": {"$in": ["Completed", "Failed"]}}, + # {"$or": [{"status": "Completed"},{"status": "Failed"}]} + {"orderDate": {"$gt": targetDate}}] + } + +returnedResult2 = db.orders.update_many(filter_condition, {"$set": {"status": "Dispute"}}) + +print(returnedResult2.modified_count) + + + # Delete + +orderID = 54321 +returnedResult3 = db.orders.delete_one({"orderID": orderID}) +print(returnedResult3.deleted_count) \ No newline at end of file