-
Notifications
You must be signed in to change notification settings - Fork 0
/
deleteone.py
37 lines (25 loc) · 1.19 KB
/
deleteone.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import os
import pprint
from pymongo import MongoClient
# Import ObjectId from bson package (part of PyMongo distribution) to enable querying by ObjectId
from bson.objectid import ObjectId
# Load config from .env file
MONGODB_URI = "mongodb://localhost:27017/?readPreference=primary&appname=MongoDB%20Compass%20Community&ssl=false"
# Connect to MongoDB cluster with MongoClient
client = MongoClient(MONGODB_URI)
# Get reference to 'bank' database
db = client.test_db
# Get a reference to the 'accounts' collection
accounts_collection = db.accounts
# Filter by ObjectId
document_to_delete = {"_id": ObjectId("64c79375f9ab894e45b90462")}
# Search for document before delete
print("Searching for target document before delete: ")
pprint.pprint(accounts_collection.find_one(document_to_delete))
# TODO: Write an expression that deletes the target account. Assign the result of this delete operation to a variable named 'result'.
result = accounts_collection.delete_one(document_to_delete)
# Search for document after delete
print("Searching for target document after delete: ")
pprint.pprint(accounts_collection.find_one(document_to_delete))
print("Documents deleted: " + str(result.deleted_count))
client.close()