-
Notifications
You must be signed in to change notification settings - Fork 5
/
mongodb.js
299 lines (249 loc) · 6.45 KB
/
mongodb.js
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/* Cheatsheet for MongoDB
Created by: Truly Mittal
Date: March 2019
*/
/* SHOW DATABASE */
show dbs
/* USE DATABASE */
use DATABASE_NAME
/* current database */
db
/* DROP(delete) DATABASE */
db.dropDatabase()
/* CREATE COLLECTION */
db.createCollection("COLLECTION_NAME", {OPTIONS})
/* DROP(delete) COLLECTION */
db.COLLECTION_NAME.drop()
/* INSERt document */
db.users.insert({
name: "Truly Mittal",
age: 29
})
/* gettings all documents */
db.COLLECTION_NAME.find()
/* pretty the output */
db.COLLECTION_NAME.find().pretty()
/* finding a document by _id */
db.users.find({ _id: ObjectId("5c4d7c52eb87a6e0e5fc8949") }).pretty()
/* Finding by a field, here it is name */
db.users.find({name: "Truly Mittal"}).pretty()
/* update document */
db.users.update(
{ name: "Truly Mittal" },
{ $set : { age: 29 } }
)
/* unset a field in document */
db.users.update(
{ name: "Truly Mittal" },
{ $unset : { age: "" } }
)
/* update and auto insert if document not present */
db.users.update(
{ name: "Truly Mittal" },
{ name: "Truly Mittal" },
{ upsert: true }
)
/* Remove a document */
db.COLLECTION_NAME.remove({query})
db.COLLECTION_NAME.deleteOne({query})
/* insert multiple documents */
db.users.insert(
[
{
name: "Jhon Doe",
age: 36,
colors: ["red", "black", "blue"],
address: {
city: "paris",
country: "france"
}
},
{
name: "Hans Down",
age: 46,
colors: ["yellow", "white"],
address: {
city: "london",
country: "uk"
}
},
{
name: "Eric Widget",
age: 31,
colors: ["red", "green", "purple"],
address: {
city: "new delhi",
country: "india"
}
},
{
name: "Indigo Violet",
age: 29,
colors: ["red", "black"],
address: {
city: "washington dc",
country: "us"
}
}
]
)
/* count the number of documents */
db.users.count() /* Number of documents */
/* limit the number of fetched records */
db.users.find().limit(2) /* limit the number of records */
/* skip the number of records */
db.users.find().skip(2)
/* sort the record by the key name (1 Asc, -1 DesC) */
db.users.find().sort({name: 1}) /* sorts the records in asc and desc */
/* Update Multiple docs */
db.users.updateMany(
{}, //Filter
{} //Updates
)
db.users.update(
{}, //Filter
{}, //Updates
{ multi: true } //required in options if using only update instead of updateMany
)
/* Change the Field Name */
db.users.updateMany(
{}, //Filter
{ $rename: { "colours" : "colors" } } //Updates
)
/* AND query */
db.users.find({
$and: [
{ name: "Indigo Violet" },
{ age: 29 }
]
})
/* OR query */
db.users.find({
$or: [
{ name: "Indigo Violet" },
{ age: 29 }
]
})
/* OR with Less than equal to query */
db.users.find({
$or: [
{ name: "John Doe" },
{ age: { $lte : 31 }}
]
})
/* not query */
db.users.find({
age: {
$not: {
$lt: 40
}
}
}).pretty()
/* array operations
adds the whole array as an element inside an array */
db.users.update(
{ name: "Truly Mittal" },
{ $addToSet : { colors: ["yellow", "pink"] } }
)
/* adds each element of the array as an element inside an array works as a SET */
db.users.update(
{ name: "Truly Mittal" },
{
$addToSet : {
colors: {
$each: ["yellow", "pink"]
}
}
}
)
/* repeat above also to show that NOTHING happens */
/* adds each element of the array as an element inside an array NOT works as a SET */
db.users.update(
{ name: "Truly Mittal" },
{
$push : {
colors: {
$each: ["yellow", "pink"]
}
}
}
)
/* removes all entries from the array */
db.users.update(
{ name: "Truly Mittal" },
{
$pull : { colors: ["yellow", "pink"] }
}
)
db.users.update(
{ name: "Truly Mittal" },
{
$pull : { colors: "yellow" } //removes all enteries of "yellow"
}
)
db.users.update(
{ name: "Truly Mittal" },
{
$pull : { colors: "pink" }
}
)
/* The $in operator selects the documents where the value of a field equals any value in the specified array. */
db.users.update(
{ },
{
$pull: {
colors: {
$in: [ "red", "black" ]
}
}
},
{ multi: true }
)
/* Increment Operator */
db.users.update(
{ name: "Truly Mittal" },
{
$inc : { age: 2 }
}
)
/* $pop */
db.users.update(
{name: "Indigo Violet" },
{ $set : {
scores: [ 3, 65, 2 ]
}}
)
db.users.update(
{name: "Indigo Violet" },
{ $pop : { scores: 1 } }
)
db.users.updateMany(
{ },
{
$push: {
colors: {
$each: [ "red", "black" ]
}
}
}
)
/* Setting the current date in field called "lastModified" */
db.inventory.updateOne(
{ name: "Truly Mittal" },
{ $currentDate: { lastModified: true } }
)
/*
The $currentDate operator sets the value of a field to the current date, either as a Date or a timestamp. The default type is Date.
{ $currentDate: { <field1>: <typeSpecification1>, ... } }
<typeSpecification> can be either:
1. a boolean true to set the field value to the current date as a Date, or
2. a document { $type: "timestamp" } or { $type: "date" } which explicitly specifies the type.
The operator is case-sensitive and accepts only the lowercase "timestamp" or the lowercase "date". */
/* Exporting in docs using mongoexport */
mongoexport --db DB_NAME --collection COLLECTION_NAME -o FILENAME.json
/* Exporting in docs using mongoexport as JSON ARRAY */
mongoexport --db DB_NAME --collection COLLECTION_NAME -o FILENAME.json --jsonArray
/* Importing Docs from a file */
mongoimport --db DB_NAME --collection COLLECTION_NAME --fileo FILENAME.json
/* Importing Docs from a file if it is formatted as a JSON ARRAY */
mongoimport --db DB_NAME --collection COLLECTION_NAME --fileo FILENAME.json --jsonArray