-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPymongoExamples.py
96 lines (88 loc) · 2.32 KB
/
PymongoExamples.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from pymongo import MongoClient
import pprint
import datetime
from bson.objectid import ObjectId
import hashlib
client = MongoClient()
client = MongoClient('localhost', 27017)
db = client.TaskManagementDb
TasksCollection = db.Tasks
UsersCollection = db.Users
#######################################################
# Inserting a Document
"""
Task = {
'title': 'Task3 title',
'description': 'task3 description',
'done': False,
'date': datetime.datetime.utcnow()
}
TaskID = TasksCollection.insert_one(Task).inserted_id
print(TaskID)
"""
"""
CryptedPassword = hashlib.md5("456".encode('utf-8')).hexdigest()
User = {
'Fullname': 'Ali Veli',
'Username': 'aliveli',
'Password': CryptedPassword,
'Role': 'Super',
'RegDate': datetime.datetime.utcnow()
}
UserID = UsersCollection.insert_one(User).inserted_id
"""
#######################################################
# Getting a Single Document With find_one()
"""
row = TasksCollection.find_one({"title": "Task2 title"})
pprint.pprint(row)
"""
"""
TaskID = "5c48368067011a95eae6b2ec"
row = TasksCollection.find_one({"_id": ObjectId(TaskID)})
pprint.pprint(row)
"""
########################################################
# Update Document
"""
TasksCollection.update_one({
'_id': ObjectId("5c495ec467011a7c2131884d")
},{
'$set': {
'title': 'Selçuk task 3 title'
}
}, upsert=False)
"""
########################################################
# Row Count
"""
RowCount = TasksCollection.count_documents({'_id': ObjectId("5c495ec467011a7c2131884d")})
"""
########################################################
# Get Multipple row
"""
for Task in TasksCollection.find({
"$or":
[
{"$or":
[
{
"UserID": ObjectId("5c48525a67011acdd517d41a"),
"done": True,
"title": {"$regex": "^baslik"}
},
{
"title": {"$regex": "task 2"},
"done": True
}
]
},
{
"UserID": ObjectId("5c48525a67011acdd517d41a"),
"done": False,
"title": {"$regex": "task 1"}
}
]
}).sort([("_id", 1)]).skip(0).limit(3):
pprint.pprint(Task)
"""