66
77Start the MongoDB Docker image via Docker:
88
9- ```
10- $ docker run --rm --name mongo-demos --detach -p 27017:27017 mongo:3.6
9+ ``` sh
10+ > docker run --rm --name mongo-demos --detach -p 27017:27017 mongo:3.6
1111```
1212
1313Don't forget to stop it when you finish:
1414
15- ```
16- $ docker stop mongo-demos
15+ ``` sh
16+ > docker stop mongo-demos
1717```
1818
1919### Maven
2020
2121Start the MongoDB docker image via Maven:
2222
23- ```
24- $ mvn docker:start
23+ ``` sh
24+ > mvn docker:start
2525```
2626
2727Don't forget the stop it once finished:
2828
29- ```
30- $ mvn docker:stop
29+ ``` sh
30+ > mvn docker:stop
3131```
3232
3333## Mongo Shell
3434
3535Install MongoDB Shell on macOS:
3636
37+ ``` sh
38+ > brew tap mongodb/brew
39+ > brew install mongodb-community-shell
40+ ```
41+
42+ Learn how to use it via the option ` -h,--help ` :
43+
3744```
38- $ brew tap mongodb/brew
39- $ brew install mongodb-community-shell
45+ > mongo -h
46+ MongoDB shell version v4.2.0
47+ usage: mongo [options] [db address] [file names (ending in .js)]
48+ db address can be:
49+ foo foo database on local machine
50+ 192.168.0.5/foo foo database on 192.168.0.5 machine
51+ 192.168.0.5:9999/foo foo database on 192.168.0.5 machine on port 9999
52+ mongodb://192.168.0.5:9999/foo connection string URI can also be used
53+ Options:
54+ ...
4055```
4156
57+ ### Interactive Mode
58+
4259Connect to MongoDB via Mongo Shell:
4360
44- ```
45- $ mongo
61+ ``` sh
62+ > mongo
4663```
4764
4865Once connected to MongDB via MongDB Shell, you can perform the following commands.
@@ -68,6 +85,20 @@ Check the MongoDB version:
68854.0.0
6986```
7087
88+ ### Background Mode
89+
90+ Evaluate a string expression and return the result in JSON format. You can also use ` jq ` to transform it to something else.
91+
92+ ``` sh
93+ mongo --quiet --eval " db.fruits.find({ ... })" | jq
94+ ```
95+
96+ Run a script and return the result in JSON format.
97+
98+ ``` sh
99+ mongo --quiet find_fruits.js
100+ ```
101+
71102## Collections
72103
73104Show collections:
@@ -90,14 +121,14 @@ Create a new collection
90121
91122Insert document(s) to collection:
92123
93- ```
124+ ``` js
94125> user1 = { " first_name" : " Foo" , " last_name" : " Bar" }
95126{ " first_name" : " Foo" , " last_name" : " Bar" }
96127> db .users .insert (user1)
97128WriteResult ({ " nInserted" : 1 })
98129```
99130
100- ```
131+ ``` js
101132> db .users .insert ([
102133... { " first_name" : " First1" , " last_name" : " Last1" },
103134... { " first_name" : " First2" , " last_name" : " Last2" },
@@ -116,19 +147,19 @@ BulkWriteResult({
116147
117148Find documents from collection:
118149
119- ```
150+ ``` js
120151> db .users .find ({ first_name: { $eq: " Foo" } })
121152{ " _id" : ObjectId (" 5e4908c14638a83462bc6a15" ), " first_name" : " Foo" , " last_name" : " Bar" }
122153{ " _id" : ObjectId (" 5e490a2a4638a83462bc6a16" ), " first_name" : " Foo" , " last_name" : " Bar2" }
123154```
124155
125- ```
156+ ``` js
126157> db .users .find ({ first_name: " Foo" })
127158{ " _id" : ObjectId (" 5e4908c14638a83462bc6a15" ), " first_name" : " Foo" , " last_name" : " Bar" }
128159{ " _id" : ObjectId (" 5e490a2a4638a83462bc6a16" ), " first_name" : " Foo" , " last_name" : " Bar2" }
129160```
130161
131- ```
162+ ``` js
132163> db .users .find ()
133164{ " _id" : ObjectId (" 5e4908c14638a83462bc6a15" ), " first_name" : " Foo" , " last_name" : " Bar" }
134165{ " _id" : ObjectId (" 5e490a2a4638a83462bc6a16" ), " first_name" : " Foo" , " last_name" : " Bar2" }
@@ -139,7 +170,7 @@ Update document(s) via
139170Although the query may match multiple documents, only one document will be
140171modified :warning : .
141172
142- ```
173+ ``` js
143174> db .users .findAndModify ({ query: {}, update: { $set: { tel: " 123" }}})
144175{
145176 " _id" : ObjectId (" 5e49118b095f13556162b497" ),
@@ -150,14 +181,14 @@ modified :warning:.
150181
151182Update documents by adding a new field "tel" for all users:
152183
153- ```
184+ ``` js
154185> db .users .updateMany ({}, { $set: { tel: " 456" }})
155186{ " acknowledged" : true , " matchedCount" : 2 , " modifiedCount" : 2 }
156187```
157188
158189Update documents by adding a nested field "address.label" for all users:
159190
160- ```
191+ ``` js
161192> db .users .updateMany ({}, { $set: { address: { label: " hi" }}})
162193{ " acknowledged" : true , " matchedCount" : 2 , " modifiedCount" : 2 }
163194
@@ -168,7 +199,7 @@ Update documents by adding a nested field "address.label" for all users:
168199
169200Remove all documents from collection (:warning : be careful!):
170201
171- ```
202+ ``` js
172203> db .users .remove ({})
173204WriteResult ({ " nRemoved" : 4 })
174205```
@@ -195,3 +226,5 @@ Default Port | Description
195226 < https://www.baeldung.com/java-mongodb >
196227- Caconde, "Install mongo shell in mac", _ Stack Overflow_ , 2019.
197228 < https://stackoverflow.com/questions/58504865/ >
229+ - Antoine Roger, "How to deal with MongoDB shell output in your shell scripts – Part 2/2: parsing JSON with jq", 2018.
230+ < https://www.mydbaworld.com/mongodb-shell-output-parsing-json-jq/ >
0 commit comments