-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
78 lines (65 loc) · 1.66 KB
/
main.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
# -*- coding: utf-8 -*-
"""CRUD - Python - PyMongo - MongoDB."""
from bson import ObjectId
from pymongo import MongoClient, ReturnDocument
client = MongoClient(
username='dbuser',
password='123456',
host='localhost',
port=27017,
authSource='admin',
)
# Conexão utilizando URI.
# username = 'dbuser'
# password = '123456'
# host = 'localhost'
# port = '27017'
# db = 'database_name'
# authSource = 'admin'
# URI = f'mongodb://{username}:{password}@{host}:{port}/{db}?authSource={authSource}'
# client = MongoClient(host=URI)
db = client['database_name']
# Remove collection.
db.drop_collection('collection_name')
# Create collection.
collection = db['collection_name']
# Create.
print('[!] Create [!]')
result = collection.insert_one(
{
'name': 'renato',
'age': 35,
},
)
obj_id = result.inserted_id
print(obj_id)
result = collection.insert_many(
[
{'name': 'maria', 'age': 25},
{'name': 'sandy', 'age': 19},
],
)
print(result.inserted_ids)
# Read.
print('\n[!] Read [!]')
print(collection.find_one({'_id': ObjectId(obj_id)}))
# Limit.
for document in collection.find().limit(3):
print(document)
# Update.
print('\n[!] Update [!]')
print(collection.find_one({'_id': ObjectId(obj_id)}))
result = collection.find_one_and_update(
{'_id': ObjectId(obj_id)},
{'$set': {'name': 'joão'}},
return_document=ReturnDocument.AFTER,
)
print(collection.find_one({'_id': ObjectId(obj_id)}))
# Delete.
print('\n[!] Delete [!]')
print(collection.find_one({'_id': ObjectId(obj_id)}))
result = collection.delete_one(
{'_id': ObjectId(obj_id)},
)
print(collection.find_one({'_id': ObjectId(obj_id)}))
client.close()