-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol_db.py
More file actions
72 lines (60 loc) · 2.28 KB
/
control_db.py
File metadata and controls
72 lines (60 loc) · 2.28 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from pymongo.mongo_client import MongoClient
from pymongo.server_api import ServerApi
from bson import ObjectId
from openai_api_key import URI
import json
uri = URI
# ObjectId를 문자열로 변환하는 함수
def convert_objectid(data):
if isinstance(data, list):
return [convert_objectid(item) for item in data]
elif isinstance(data, dict):
return {key: convert_objectid(value) for key, value in data.items()}
elif isinstance(data, ObjectId):
return str(data)
else:
return data
client = MongoClient(uri, server_api=ServerApi('1'))
db = client['intheview']
while True:
case_num = input("just check : 1\ndelete : 2\n: ")
if case_num == "1":
# 컬렉션 목록 가져오기
collections = db.list_collection_names()
for collection_name in collections:
collection = db[collection_name]
documents = collection.find()
print(f"\nCollection: {collection_name}\n" + "-"*60)
for document in documents:
document = convert_objectid(document)
document_str = json.dumps(document, indent=4, ensure_ascii=False)
print(document_str)
print("-"*60)
elif case_num == "2":
coll = input("which collection : ")
collection = db[coll]
while True:
collections = db.list_collection_names()
for collection_name in collections:
collection = db[collection_name]
documents = collection.find()
print(f"\nCollection: {collection_name}\n" + "-"*60)
for document in documents:
document = convert_objectid(document)
document_str = json.dumps(document, indent=4, ensure_ascii=False)
print(document_str)
print("-"*60)
oid = input("Object Id to remove : ")
if oid == 1:
break
if coll == "sessions":
db['users'].update_many(
{},
{"$pull": {"sessions": ObjectId(oid)}}
)
collection.delete_one({"_id": ObjectId(oid)})
# 컬렉션 목록 가져오기
elif case_num=="0":
break
else:
print("Invalid input. Please enter 1 or 2.")