|
2 | 2 |
|
3 | 3 | ## 02_db |
4 | 4 |
|
5 | | -### create |
| 5 | +#### create |
| 6 | + |
6 | 7 | ``` |
7 | 8 | use <db name> |
8 | 9 | ``` |
| 10 | +The above will use the database of the provided name if it exists, and create it if it doesn't |
| 11 | + |
| 12 | +#### use |
9 | 13 |
|
10 | | -### use |
11 | 14 | ``` |
12 | 15 | use <db name> |
13 | 16 | ``` |
14 | 17 |
|
15 | | -### see current db |
| 18 | +#### example |
| 19 | +``` |
| 20 | +use temp |
| 21 | +``` |
| 22 | + |
| 23 | +The above will use the database of the provided name if it exists, and create it if it doesn't |
| 24 | + |
| 25 | +#### see current db |
| 26 | + |
16 | 27 | ``` |
17 | 28 | db |
18 | 29 | ``` |
19 | 30 |
|
20 | | -### see all db |
| 31 | +#### see all db |
| 32 | + |
21 | 33 | ``` |
22 | 34 | show dbs |
23 | 35 | ``` |
| 36 | +You need to have at least one document in a db for it to be seen. |
| 37 | + |
| 38 | +#### insert document |
24 | 39 |
|
25 | | -### insert document |
26 | 40 | ``` |
27 | 41 | db.<collection name>.insert({"name":"McLeod"}) |
28 | 42 | ``` |
29 | | -If the collection doesn't exist, it is created |
30 | 43 |
|
31 | | -### view collections |
| 44 | +example |
32 | 45 | ``` |
33 | | -show collections |
| 46 | +db.dogs.insert({"name":"toby"}) |
34 | 47 | ``` |
35 | 48 |
|
36 | | -### view documents |
| 49 | +#### view documents |
37 | 50 | ``` |
38 | 51 | db.<collection name>.find() |
39 | 52 | ``` |
40 | 53 |
|
41 | | -### drop db |
| 54 | +example |
| 55 | +``` |
| 56 | +db.cats.insert({"firstname":"coco"}) |
| 57 | +``` |
| 58 | + |
| 59 | +``` |
| 60 | +db.cats.find().pretty() |
| 61 | +``` |
| 62 | + |
| 63 | +#### view collections |
| 64 | +``` |
| 65 | +show collections |
| 66 | +``` |
| 67 | + |
| 68 | +#### drop db |
42 | 69 | ``` |
43 | 70 | db.dropDatabase() |
44 | 71 | ``` |
45 | 72 |
|
46 | 73 | ## 03_collection |
47 | 74 |
|
48 | | -### create implicitly |
| 75 | +# collection commands |
| 76 | + |
| 77 | +#### create implicitly |
49 | 78 | ``` |
50 | 79 | db.<collection name>.insert({"name":"McLeod"}) |
51 | 80 | ``` |
52 | 81 |
|
53 | | -### create explicitly |
| 82 | +#### create explicitly |
54 | 83 | ``` |
55 | | -db.createCollection(<name>, <optional options>) |
| 84 | +db.createCollection(<name>, {<optional options>}) |
56 | 85 | ``` |
57 | 86 |
|
58 | 87 | #### optional options |
59 | 88 | | option | type | description | |
60 | 89 | | --- | --- | --- | |
61 | | -| autoindex | bool | creates _id index automatically | |
62 | 90 | | capped | bool | caps the size | |
63 | 91 | | size | number | sets size of cap in bytes | |
64 | 92 | | max | bool | maximum number of documents allowed in capped collection | |
65 | 93 |
|
| 94 | +[other options including validation](https://docs.mongodb.com/manual/reference/method/db.createCollection/) |
| 95 | + |
66 | 96 | #### examples |
67 | 97 | ``` |
68 | 98 | db.createCollection("customers") |
69 | 99 | ``` |
70 | 100 |
|
71 | 101 | ``` |
72 | | -db.createCollection("customers",{autoindex:true}) |
73 | | -``` |
74 | | - |
75 | | -``` |
76 | | -db.createCollection("customers",{autoindex:true, capped:true,size:65536,max:1000000}) |
| 102 | +db.createCollection("crs",{capped:true, size:65536,max:1000000}) |
77 | 103 | ``` |
78 | 104 |
|
79 | | -### view collections |
| 105 | +#### view collections |
80 | 106 | ``` |
81 | 107 | show collections |
82 | 108 | ``` |
83 | 109 |
|
84 | | -### drop |
| 110 | +#### drop |
85 | 111 | ``` |
86 | 112 | db.<collection name>.drop() |
87 | | -
|
88 | 113 | ``` |
89 | 114 |
|
90 | 115 | ## 04_document |
91 | 116 |
|
92 | | -### insert |
93 | | -``` |
94 | | -db.<collection name>.insert({document}) |
95 | | -``` |
96 | | - |
97 | | -### insert multiple |
98 | | -``` |
99 | | -db.<collection name>.insert(< [{document}, {document}, ..., {document}] >) |
100 | | -``` |
101 | | -pass in an array of documents |
102 | 117 |
|
103 | 118 | ## 05_query |
104 | | - |
105 | | -### find |
106 | | -``` |
107 | | -db.<collection name>.find() |
108 | | -``` |
109 | | - |
110 | | -### find one |
111 | | -``` |
112 | | -db.<collection name>.findOne() |
113 | | -``` |
114 | | - |
115 | | -### pretty |
116 | | -``` |
117 | | -db.<collection name>.find().pretty() |
118 | | -``` |
119 | | -pretty prints the results |
120 | | - |
121 | | -### operators |
122 | | - |
123 | | -| operator | syntax | example | |
124 | | -| --- | --- | --- | |
125 | | -| equality | {key:value} | db.customers.find({"name":"McLeod"}).pretty() | |
126 | | -| less than | {key:{$lt:value}} | db.customers.find({"age":{$lt:20}}).pretty() | |
127 | | -| less than equals | {key:{$lte:value}} | db.customers.find({"age":{$lte:20}}).pretty() | |
128 | | -| greater than | {key:{$gt:value}} | db.customers.find({"age":{$gt:20}}).pretty() | |
129 | | -| greater than equals | {key:{$gte:value}} | db.customers.find({"age":{$gte:20}}).pretty() | |
130 | | -| not equals | {key:{$ne:value}} | db.customers.find({"age":{$ne:20}}).pretty() | |
131 | | - |
132 | | -### and |
133 | | -``` |
134 | | -db.customers.find({$and: [{"name":"McLeod"}, {"age":20}]}).pretty() |
135 | | -db.customers.find({$and: [{"name":"McLeod"}, {"age":{$lt:20}}]}).pretty() |
136 | | -db.customers.find({$and: [{"name":"McLeod"}, {"age":{$gt:20}}]}).pretty() |
137 | | -``` |
138 | | - |
139 | | -### or |
140 | | -``` |
141 | | -db.customers.find({$or: [{"name":"McLeod"}, {"age":20}]}).pretty() |
142 | | -db.customers.find({$or: [{"name":"McLeod"}, {"age":{$lt:20}}]}).pretty() |
143 | | -db.customers.find({$or: [{"name":"McLeod"}, {"age":{$gt:20}}]}).pretty() |
144 | | -``` |
145 | | - |
146 | | -### and or |
147 | | -``` |
148 | | -db.customers.find({"role":"owner", $or: [{"name":"McLeod"}, {"age":20}]}).pretty() |
149 | | -db.customers.find({"role":"owner", $or: [{"name":"McLeod"}, {"age":{$lt:20}}]}).pretty() |
150 | | -db.customers.find({"role":"owner", $or: [{"name":"McLeod"}, {"age":{$gt:20}}]}).pretty() |
151 | | -``` |
152 | | -WHERE role = 'owner' AND (name = 'mcleod' OR age = 20) |
153 | | - |
0 commit comments