10000 pymongo tutorial for crud operation · premaseem/pythonLab@2e410a5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2e410a5

Browse files
committed
pymongo tutorial for crud operation
1 parent d0a1ea5 commit 2e410a5

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""
2+
Link: https://www.w3schools.com/python/python_mongodb_create_db.asp
3+
4+
"""
5+
6+
import pymongo
7+
8+
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
9+
10+
mydb = myclient["mydatabase"]
11+
12+
mycol = mydb["customers"]
13+
14+
15+
mydict = {
16+
"name": "John",
17+
"address": "Highway 37"
18+
}
19+
20+
mylist = [
21+
{ "name": "Amy", "address": "Apple st 652"},
22+
{ "name": "Hannah", "address": "Mountain 21"},
23+
{ "name": "Michael", "address": "Valley 345"},
24+
{ "name": "Sandy", "address": "Ocean blvd 2"},
25+
{ "name": "Betty", "address": "Green Grass 1"},
26+
{ "name": "Richard", "address": "Sky st 331"},
27+
{ "name": "Susan", "address": "One way 98"},
28+
{ "name": "Vicky", "address": "Yellow Garden 2"},
29+
{ "name": "Ben", "address": "Park Lane 38"},
30+
{ "name": "William", "address": "Central st 954"},
31+
{ "name": "Chuck", "address": "Main Road 989"},
32+
{ "name": "Viola", "address": "Sideway 1633"},
33+
{ "name": "Aseem", "address": "Sideway 1633" , "age": 16}
34+
]
35+
# x = mycol.insert(mylist)
36+
37+
# print(x)
38+
39+
# without projection
40+
# x = mycol.find_one({ "name": "Aseem" } )
41+
42+
myquery = { "address": "Valley 345" }
43+
newvalues = { "$set": { "address": "Canyon 123" } }
44+
45+
mycol.update(myquery, newvalues)
46+
47+
# with projection
48+
# result_set = mycol.find({ "age": { "$lt": 18 } } , { "name":1 , "address": 1 , "_id":0})
49+
result_set = mycol.find().sort("name", 1)
50+
for rec in result_set:
51+
print(rec)
52+

0 commit comments

Comments
 (0)
0