我想要打印我刚刚删除的内容
- 主要功能
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)
- 产出
我想打印我刚刚删除的东西,我删除了两个{ "name": "Joy22" }
数据,但我只打印了集合顶部的2个数据,MongoDB有一些例子,但是很难找到同样情况下的pymongo调用
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'>
- 完整代码,以防有人想看
# 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))
1条答案
按热度按时间fafcakar1#
DeleteResult
对象仅返回删除的项目数。一旦某项被删除,它将不再出现在集合中,因此以下代码片段:将返回前两个剩余项,这意味着有两个名为
joy22
的文档(因此修改后的计数为2)。您将注意到,返回的两个文档没有名称joy22
。因此,它们不是已删除的文档。要实现您的目标,首先是
find
您的文档,然后是delete_many
,然后打印find
的结果。