-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo.py
62 lines (49 loc) · 2.11 KB
/
mongo.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
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
import pymongo
from pymongo import MongoClient
client = MongoClient()
db = client["chess-analysis"]
players = db["players"]
def deleteCollection(collection):
result = collection.delete_many({})
# Print information about the deletion
print(f"Deleted {result.deleted_count} documents from the collection.")
def pushArray(collection, player, array_name, data_to_push):
# Update the document, pushing the data to the array
result = collection.update_one(
{"username": player}, # Specify the document using its identifier
# Use $push to add data to the array
{"$push": {array_name: data_to_push}}
)
def get_element_at_index(collection, username, array_field, game_index):
# Find the document by ID and retrieve the element at the specified index
result = collection.find_one({"username": username})
if result and array_field in result:
element_at_index = result[array_field][game_index] if game_index < len(
result[array_field]) else None
else:
element_at_index = None
return element_at_index
def insertIfNotExist(collection, property_name, property_value, player_doc):
# Check if a document with the specified property name exists
existing_document = collection.find_one({property_name: property_value})
# If no document is found, insert a new one
if existing_document is None:
new_document = {
property_name: property_value
}
collection.update_one(player_doc, # Search criteria
# Update or insert the document
{'$set': new_document},
# Set to True for upsert (update or insert)
upsert=True
)
print("Document inserted.")
else:
print("Document with the specified property name already exists.")
def findAll(collection):
# Find all documents in the collection
documents = list(collection.find())
return documents
def getPlayer(collection, username):
docs = list(collection.find({"username": username}))
return docs[0]