I want to print what I just deleted by pymongo
- the main function
def del_data(del_values_json ,one_or_many_bool ):
if one_or_many_bool:
x = mycol.delete_many(del_values_json)
else:
x = mycol.delete_one(del_values_json)
retrun x
number_of_delete_data = int(x.deleted_count)
modified_data_list = []
for modified_data in mycol.find().sort("_id", -1).limit(number_of_delete_data):
print(modified_data)
modified_data_list.append(modified_data)
- the output
I want to print what I just deleted, I delete two { "name": "Joy22" }data, but I just print the top of 2 data in the collection, MongoDB has some examples, however, is hard to find pymongo call for the same case
Enter the IP: localhost.173
Enter exist DB name: (practice_10_14)-0002
Enter exist collection name: collection_new02cp
U are deleted one or many values? (ex:1 for many , 0 for one): 1
Enter delete values: { "name" : "Joy22" }
<pymongo.results.DeleteResult object at 0x00000282A37C6890>
2 documents delete.
{'_id': ObjectId('6350c8a3a1fa85dd0cfe590a'), 'name': 'Joy', 'ID': 100998, 'Age': 23, 'time': DateTime.datetime(2022, 10, 17, 9, 11, 54)}
{'_id': ObjectId('6350c8a3a1fa85dd0cfe5909'), 'name': 'Joy', 'ID': 100998, 'Age': 23, 'time': DateTime.datetime(2022, 10, 17, 9, 11, 54)}
{'ok': 1, 'msg': 'no error occur', 'count': 2}
<class 'str'>
- the whole code in case someone wants to take a look
# Delete collection/table
import pymongo
import datetime
import json
from bson.objectid import ObjectId
from bson import json_util
def init_db(ip, db, coll):
try:
myclient = pymongo.MongoClient('mongodb://' + ip + '/')
mydb = myclient[db]
mycol = mydb[coll]
except Exception as e:
msg_fail_reason = "error in init_db function"
return msg_fail_reason
return mydb, mycol
# del_data = delete_db_data
# one_or_many_bool: input 1 means True; input 0 is False
def del_data(del_values_json ,one_or_many_bool ):
try:
if one_or_many_bool:
x = mycol.delete_many(del_values_json)
else:
x = mycol.delete_one(del_values_json)
except Exception as e:
msg_fail_reason = "error in del_data function"
return msg_fail_reason
return x
msg_fail_reason = "no error occur"
ip_input = input("Enter the ip: ")
exist_DB_name = input("Enter exist DB name: ")
exist_coll_name = input("Enter exist collection name: ")
mydb, mycol = init_db(ip_input, exist_DB_name, exist_coll_name)
insert_one_or_many = input("U are delete one or many values? (ex:1 for many , 0 for one): ")
del_values_str = input("Enter delete values: ")
one_or_many_bool = bool(int(insert_one_or_many))
del_values_json =json.loads(del_values_str)
x = del_data(del_values_json ,one_or_many_bool )
print(x)
print(x.deleted_count, "documents delete.")
number_of_delete_data = int(x.deleted_count)
modified_data_list = []
for modified_data in mycol.find().sort("_id", -1).limit(number_of_delete_data):
print(modified_data)
modified_data_list.append(modified_data)
def parse_json(data):
return json.loads(json_util.dumps(data))
# if someone wants data in JSON
# modified_data_json = parse_json(modified_data_list)
# 1 means success
return_status_str = { "ok" : 1 , "msg" : msg_fail_reason , "count" : number_of_delete_data}
print(return_status_str)
# return_status_json is json data, dump
return_status_json = (json.dumps(return_status_str))
print(type(return_status_json))
The
DeleteResultobject only returns the number of items deleted. Once an item is deleted it will no longer occur in the collection so this fragment of code:Will return the first two remaining items which implies there were two documents with the name
joy22(hence a modified count of 2). You will note that the two documents returned do not have the namejoy22. Therefore they are not the deleted documents.To achieve your goal first
findyour documents, thendelete_manythen print the results of thefind.