forked from mnegi/training-nvp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongodb-commands.txt
179 lines (138 loc) · 3.7 KB
/
mongodb-commands.txt
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
MongoDB server: mongod
MongoDB client: mongo
1. List databases
show dbs
2. Use db (switch to the database)
use DATABASE_NAME
Note! if the db exists, it will switch to it, if not it will still switch and it assumes you are creating a new db
3. Show collections
show collections
4. Create a collections
db.createCollection("users")
5. Insert a document to the collections
db.users.insertOne({
"name": "Manohar Negi",
"email": "mnegi@abc.com",
"password": "supersecret",
"address": "JP Nagar, Bangalore",
"mobile": 789263311
})
db.users.insertOne({
"name": "MS Negi",
"email": "mnegi@abc.com",
"password": "supersecret",
"address": "JP Nagar 1st Phase",
"mobile": 789263311
})
db.users.insertOne({
"name": "Rahul",
"email": "rahul@abc.com",
"password": "supersecret",
"address": "Koramangala",
"mobile": 8908908908
})
db.users.insertOne({
"name": "Viraj",
"email": "viraj@abc.com",
"password": "supersecret",
"address": "BTM",
"mobile": 9886681566,
"age": 25
})
6. Finding the documents
Find all
db.users.find()
Filter by name
db.users.find({name: "Manohar Negi"})
7. Projections - getting the colums
db.users.find(
{ },
{ email: 1, password: 1 , mobile: 0}
)
8. Conditions
// mobile not equal (ne)
db.users.find(
{ mobile: { $ne: 789263311 } }
)
// AND criteria
db.users.find(
{password: "supersecret", mobile: 789263311}
)
// OR criteria
db.users.find(
{ $or: [ {password: "supersecret"} , { mobile: 789263311 } ] }
)
db.users.find(
{ $or: [ {mobile: 8908908908} , { mobile: 789263311 } ] }
)
// greater than (gt)
db.users.find(
{ age: { $gt: 25 } }
)
// less than (lt)
db.users.find(
{ age: { $lt: 25 } }
)
// lte = less than or equal
db.users.find(
{ age: { $gt: 25, $lte: 50 } }
)
//WHERE name like "%ka%"
db.users.find( { name: /ka/ } )
// WHERE name like "Vi%"
db.users.find( { name: /^Vi/ } )
or
db.users.find( { name: { $regex: /^Vi/ } } )
9. Sorting
//ORDER BY name ASC
db.users.find( ).sort({ name: 1 })
//ORDER BY name DESC
db.users.find( ).sort({ name: -1 })
10. Count
//SELECT COUNT(*)
db.users.count()
//SELECT COUNT(user_id)
db.users.count( { status: { $exists: true } } )
db.users.count( { status: "inactive" } )
db.users.find( { age: { $gt: 30 } } ).count()
11. distinct
// SELECT DISTINCT(status)
db.users.distinct( "status" )
12. Limit
//LIMIT 1
db.users.findOne()
or
db.users.find().limit(1)
//LIMIT 5 SKIP 10
db.users.find().limit(5).skip(10)
total documents count 100
page size = 10 (total pages = 100/10 = 10 )
first page
db.users.find().limit(10).skip(0)
second page
db.users.find().limit(10).skip( (page number - 1) * page size)
13. Explain the query
db.users.find( { status: "active" } ).explain()
14. Update the document
//ADD status
db.users.updateMany(
{ },
{ $set: { create_at: new Date() } }
)
db.users.updateMany(
{ },
{ $set: { age: 30 } }
)
// modify the existing
db.users.updateMany({ name: { $regex: /^Vi/ } } ,{ $set: {status: 'inactive'}})
//DROP COLUMN j
db.users.updateMany(
{ },
{ $unset: { "join_date": "" } }
)
15. Create index
db.users.createIndex( { email: 1 } )
16. Delete the collection
db.users.drop()
17. Drop the database
db.dropDatabase()