Skip to content

Commit 7ec1dd4

Browse files
committed
you're doing great
1 parent ae82e76 commit 7ec1dd4

File tree

3 files changed

+200
-2
lines changed

3 files changed

+200
-2
lines changed

046_mongodb/05_query/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ db.customers.find({$or:[
6060

6161
### regex
6262
```
63-
db.crayons.find({name: {$regex: '^M'}})
63+
db.customers.find({name: {$regex: '^M'}})
6464
```
6565

6666
[regex cheatsheet](regex.pdf)

046_mongodb/06_update/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ save will overwrite a record
66

77
### update
88
```
9-
db.<collection mame>.update(<selection criteria>, <update data>)
9+
db.<collection mame>.update(<selection criteria>, <update data>, <optional options>)
1010
```
1111

1212
example
@@ -32,6 +32,10 @@ db.customers.update({_id:ObjectId("5891221756867ebff44cc886")},{$set:{role:"doub
3232
db.customers.update({name:"Moneypenny"},{$set:{role:"citizen"}})
3333
```
3434

35+
```
36+
db.customers.update({name:"Moneypenny"},{$set:{role:"double-zero", name: "Miss Moneypenny"}})
37+
```
38+
3539
```
3640
db.customers.update({age:{$gt:35}},{$set:{role:"double-zero"}})
3741
```

046_mongodb/README.md

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,197 @@ db.dropDatabase()
196196
```
197197

198198
## 05_query
199+
200+
# query
201+
202+
### setup
203+
204+
```
205+
use store
206+
db
207+
show dbs
208+
db.customers.insert([{"role":"double-zero","name": "Bond","age": 32},{"role":"citizen","name": "Moneypenny","age":32},{"role":"citizen","name": "Q","age":67},{"role":"citizen","name": "M","age":57},{"role":"citizen","name": "Dr. No","age":52}])
209+
```
210+
211+
### find
212+
```
213+
db.<collection name>.find()
214+
db.customers.find()
215+
```
216+
217+
### find one
218+
```
219+
db.<collection name>.findOne()
220+
db.customers.findOne()
221+
```
222+
223+
### find specific
224+
```
225+
db.customers.find({"name":"Bond"})
226+
db.customers.find({name:"Bond"})
227+
```
228+
You can do it either way: ```"name" or name```. JSON specification is to enclose name (object name-value pair) in double qoutes
229+
230+
### and
231+
```
232+
db.customers.find({$and: [{name:"Bond"}, {age:32}]})
233+
db.customers.find({$and: [{name:"Bond"}, {age:{$lt:20}}]})
234+
db.customers.find({$and: [{name:"Bond"}, {age:{$gt:20}}]})
235+
```
236+
237+
### or
238+
```
239+
db.customers.find({$or: [{name:"Bond"}, {age:67}]})
240+
db.customers.find({$or: [{name:"Bond"}, {age:{$lt:20}}]})
241+
db.customers.find({$or: [{name:"Bond"}, {age:{$gt:32}}]})
242+
```
243+
244+
### and or
245+
```
246+
db.customers.find({role:"citizen"})
247+
db.customers.find({age:52})
248+
db.customers.find({$and: [{role:"citizen"}, {age:52}]})
249+
db.customers.find({$or: [{role:"citizen"}, {age:52}]})
250+
db.customers.find({$or: [{role:"citizen"}, {age:52}, {name:"Bond"}]})
251+
```
252+
253+
```
254+
db.customers.find({$or:[
255+
{ $and : [ { role : "citizen" }, { age : 32 } ] },
256+
{ $and : [ { role : "citizen" }, { age : 67 } ] }
257+
]})
258+
```
259+
260+
### regex
261+
```
262+
db.customers.find({name: {$regex: '^M'}})
263+
```
264+
265+
[regex cheatsheet](05_query/regex.pdf)
266+
267+
### pretty
268+
```
269+
db.<collection name>.find().pretty()
270+
```
271+
pretty prints the results
272+
273+
### operators
274+
275+
| operator | syntax | example |
276+
| --- | --- | --- |
277+
| equality | {key:value} | db.customers.find({"name":"Bond"}).pretty() |
278+
| less than | {key:{$lt:value}} | db.customers.find({"age":{$lt:20}}).pretty() |
279+
| less than equals | {key:{$lte:value}} | db.customers.find({"age":{$lte:20}}).pretty() |
280+
| greater than | {key:{$gt:value}} | db.customers.find({"age":{$gt:20}}).pretty() |
281+
| greater than equals | {key:{$gte:value}} | db.customers.find({"age":{$gte:20}}).pretty() |
282+
| not equals | {key:{$ne:value}} | db.customers.find({"age":{$ne:20}}).pretty() |
283+
284+
285+
# JSON reminder
286+
JavaScript Object Notation (JSON) is a text format for the serialization of structured data.
287+
288+
It is derived from the object literals of JavaScript.
289+
290+
JSON can represent four primitive types (strings, numbers, booleans, and null) and two structured types (objects and arrays)
291+
292+
#### Primitive JSON
293+
294+
Here are four small JSON texts containing only values:
295+
296+
```
297+
"Hello world!"
298+
42
299+
true
300+
null
301+
```
302+
303+
#### Object JSON
304+
305+
An object structure is represented as a pair of curly brackets surrounding zero or more **name-value** pairs (or members).
306+
307+
An object is an unordered collection of zero or more **name:value** pairs
308+
309+
A **name** is a string
310+
311+
A **value** is a string, number, boolean, null, object, or array.
312+
313+
Declare properties using **name:value** pairings separated by commas
314+
315+
Enclose names in curly braces
316+
317+
There is no trailing comma
318+
319+
This is a JSON object:
320+
321+
```
322+
{
323+
"Image": {
324+
"Width": 800,
325+
"Height": 600,
326+
"Title": "View from 15th Floor",
327+
"Thumbnail": {
328+
"Url": "http://www.example.com/image/481989943",
329+
"Height": 125,
330+
"Width": 100
331+
},
332+
"Animated" : false,
333+
"IDs": [116, 943, 234, 38793]
334+
}
335+
}
336+
```
337+
338+
#### Array JSON
339+
340+
An array structure is represented as square brackets surrounding zero or more values (or elements).
341+
342+
Elements are separated by commas.
343+
344+
A value must be an
345+
346+
```
347+
object
348+
array
349+
number
350+
string
351+
three literal names
352+
true
353+
false
354+
null
355+
```
356+
357+
This is a JSON array containing two objects:
358+
359+
[
360+
{
361+
"precision": "zip",
362+
"Latitude": 37.7668,
363+
"Longitude": -122.3959,
364+
"Address": "",
365+
"City": "SAN FRANCISCO",
366+
"State": "CA",
367+
"Zip": "94107",
368+
"Country": "US"
369+
},
370+
{
371+
"precision": "zip",
372+
"Latitude": 37.371991,
373+
"Longitude": -122.026020,
374+
"Address": "",
375+
"City": "SUNNYVALE",
376+
"State": "CA",
377+
"Zip": "94085",
378+
"Country": "US"
379+
}
380+
]
381+
382+
#### Number
383+
384+
The representation of numbers is similar to that used in most programming languages. A number is represented in base 10 using decimal digits. It contains an integer component that may be prefixed with an optional minus sign, which may be followed by a fraction part and/or an exponent part. Leading zeros are not allowed. A fraction part is a decimal point followed by one or more digits.
385+
386+
#### String
387+
388+
The representation of strings is similar to conventions used in the C family of programming languages. A string begins and ends with **double quotation marks**.
389+
390+
source: The Internet Engineering Task Force (IETF)
391+
392+
## 06_update

0 commit comments

Comments
 (0)