forked from mongodb-developer/python-mongodb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpymongo_test_insert.py
61 lines (49 loc) · 1.63 KB
/
pymongo_test_insert.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
def get_database():
from pymongo import MongoClient
import pymongo
# Provide the mongodb atlas url to connect python to mongodb using pymongo
CONNECTION_STRING = 'mongodb+srv://<username>:<password>@<cluster-name>.mongodb.net/myFirstDatabase'
# Create a connection using MongoClient. You can import MongoClient or use pymongo.MongoClient
from pymongo import MongoClient
client = MongoClient(CONNECTION_STRING)
# Create the database for our example (we will use the same database throughout the tutorial
return client['user_shopping_list']
# Add this to execute only the called functions from other files
if __name__ == "__main__":
# Get the database
dbname = get_database()
# Create a new collection
collection_name = dbname["user_1_items"]
# Create the first document
item_1 = {
"_id" : "U1IT00001",
"item_name" : "Blender",
"max_discount" : "10%",
"batch_number" : "RR450020FRG",
"price" : 340,
"category" : "kitchen appliance"
}
# Create the second document
item_2 = {
"_id" : "U1IT00002",
"item_name" : "Egg",
"category" : "food",
"quantity" : 12,
"price" : 36,
"item_description" : "brown country eggs"
}
# Insert both the documents at once using insert_many()
collection_name.insert_many([item_1,item_2])
# Parsing date for the third document
from dateutil import parser
expiry_date = '2021-07-13T00:00:00.000Z'
expiry = parser.parse(expiry_date)
# Create document 3
item_3 = {
"item_name" : "Bread",
"quantity" : 2,
"ingredients" : "all-purpose flour",
"expiry_date" : expiry
}
# Insert single document
collection_name.insert_one(item_3)