1
+ from pymongo import MongoClient
2
+
3
+ client = MongoClient ("mongo://localhost:3000/" )
4
+ db = client ['mydatabase' ]
5
+ mycol = db ['mycollection' ]
6
+
7
+
8
+ #CURD
9
+ #insert_one() insert_many()
10
+ #update_one() update_many()
11
+ #find() find_one()
12
+ #delete_one() delete_many()
13
+
14
+ data1 = {
15
+ "name" : "abhi" ,
16
+ "class" : "mca" ,
17
+ "div" : "B"
18
+ }
19
+
20
+ data2 = [{"name" :"abhi" ,"class" :"mca" ,"div" :"B" },
21
+ {"name" :"satya" ,"class" :"mcs" ,"div" :"A" },
22
+ {"name" :"abhinav" ,"class" :"msc" ,"div" :"C" }
23
+ ]
24
+
25
+ #create
26
+ #mycol.inset_one(data1)
27
+ #mycol.inset_many(data2);
28
+
29
+ #read
30
+ all_collection = mycol .find ()
31
+ for x in all_collection :
32
+ print (x ) #display all entries
33
+
34
+ for x in mycol .find_one ():
35
+ print (x ) #display first entry
36
+
37
+
38
+ #update
39
+ my_query = {"name" ,"abhinav" }
40
+ my_values = {"$set" ,{"class" : "MCA" }}
41
+
42
+ #update a single document
43
+ mycol .update_one (my_query ,my_values )
44
+
45
+ #update the multiple entiries
46
+ mycol .update_many ("$set" ,{"class" :"MCA" })
47
+
48
+ #delete
49
+ #delete one
50
+ mycol .delete_one (my_query )
51
+
52
+ #delete many
53
+ mycol .delete_many (my_query )
0 commit comments