-
Notifications
You must be signed in to change notification settings - Fork 1
/
mongoDB_commands.properties
141 lines (97 loc) · 3.89 KB
/
mongoDB_commands.properties
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#start database server
#mongodb runs as background service in windows
mongod
#to start mongo client
mongo
show dbs
show collections
#create and use shop database
use shop
#insert a record
#products is the collection equivalent to table
#record is the document
#MongoDB generates unique id for every record
db.products.insertOne({name:"A book",price:10.99})
#find the documents
db.products.find()
db.products.find().pretty()
#insert multilevel documents
db.products.insertOne({name:"computer",price:999,description:"New Computer",specs:{cpu:"intel i5",ram:"8 GB",SDD:"512 GB"}})
#insert id manually
db.products.insertOne({name:"computer",price:999,description:"New Computer",specs:{cpu:"intel i5",ram:"8 GB",SDD:"512 GB"},_id:"12345"})
db.products.insertMany([
{name:"computer",price:999,description:"New Computer",specs:{cpu:"intel i5",ram:"8 GB",SDD:"512 GB"},_id:"1"},
{name:"computer",price:999,description:"New Computer",specs:{cpu:"intel i5",ram:"8 GB",SDD:"512 GB"},_id:"2"}
]
)
#CRUD_operations API
insertOne(data,options)
insertMany(data,options)
find(filter,options)
findOne(filter,options)
updateOne(filter,data,options)
updateMany(filter,data,options)
deleteOne(filter,options)
deleteMany(filter,options)
db.products.deleteOne({_id:"12345"})
#delete multiple records with matching field value
db.products.deleteMany({marker:"to_delete"})
#delete all records
db.products.deleteMany({})
#set keyword is sue for update
db.products.updateOne({_id:"12345"},{$set: {marker: "delete"}} )
db.products.updateMany({},{$set: {marker: "to_delete"}})
#Update replaces the entire document.
db.products.update({_id:"12345"},{{marker: "delete"}})
#find many records with condition
db.products.find({_id:"1"}).pretty()
#greater than condition
db.products.find({price:{$gt:999}}).pretty()
#Cursor
- to retrieve data on demand. not loading all data from db
#Projection
#filter search results
#id is included by default
db.passengers.find({},{name: "dinesh"}).pretty()
#Embedded Documents
- nested document in 1 collection
- upto 100 levels of nesting
- max 16 MB/document limit
db.passengers.updateMany({},{$set: {status: {description: "on-time",lastupdate: "1 hr ago"}}})
db.passengers.updateMany({},{$set: {status: {description: "on-time",lastupdate: "1 hr ago"},details: {res: "a",EO:{a:"1"}}}})
db.products.find({"specs.cpu": "intel i5"}).pretty()
#Arrays
- can hold any data
db.passengers.updateMany({},{$set: {status: {description: "on-time",lastupdate: "1 hr ago"}}})
db.passengers.updateMany({},{$set: {status: {description: "on-time",lastupdate: "1 hr ago"},details: [res: "a",EO:{a:"1"}]}})
db.products.findOne({_id: "1"}).hobbies
db.products.find({hobbies: "betting"}).pretty()
#Drop_database
db.dropDatabase()
#Drop collection
db.myCollection.drop()
#Indexes
db.cars.createIndex({price: 40000})
db.cars.getIndexes()
db.cars.dropIndexes()
#Replication
#create 3 mongo server cluster with 1 primary and 2 secondary
mongod --port 27017 --dbpath "C:\Users\keeth\Desktop\Dinesh-Spring-boot\mongoDB\mongo-replication\data0" --replSet "rs0" --bind_ip localhost
mongod --port 27018 --dbpath "C:\Users\keeth\Desktop\Dinesh-Spring-boot\mongoDB\mongo-replication\data1" --replSet "rs0" --bind_ip localhost
mongod --port 27019 --dbpath "C:\Users\keeth\Desktop\Dinesh-Spring-boot\mongoDB\mongo-replication\data2" --replSet "rs0" --bind_ip localhost
client side
# this will connect to 27017 by default
mongo
#this wil initiate the replica set and add 27017 to the replica set
rs.initiate()
#Add the other 2 nodes in the replica set
rs.add("localhost:27018")
rs.add("localhost:27019")
#Transaction
#Session
const session = db.getMongo().startSession()
const usersC = session.getDatabase("products").users
const postsC = session.getDatabase("products").posts
usersC.deleteOne({_id:ObjectId("5ef847a1de1315360953c96a")})
postsC.deleteOne({userId : ObjectId("5ef847a1de1315360953c96a")})
session.commitTransaction()