1
+ :template: instruqt
2
+
1
3
.. _getting-started:
2
4
3
5
===============
@@ -11,6 +13,8 @@ database and querying that data using the documentation's embedded
11
13
web shell. You do not need to deploy or install MongoDB to complete
12
14
this tutorial.
13
15
16
+ .. instruqt:: /mongodb-docs/tracks/getting-started-with-mongodb?token=em_j_d_wUT93QTFvgsZ
17
+
14
18
The examples in this tutorial use a subset of the
15
19
:atlas:`Sample Mflix Dataset </sample-data/sample-mflix/>`, which is
16
20
part of the sample data included in MongoDB's cloud-hosted service,
@@ -19,288 +23,6 @@ Atlas requires no installation overhead and offers a free tier to get
19
23
started. After completing this tutorial, you can use Atlas to
20
24
explore additional sample data or host your own data.
21
25
22
- .. _mongo-web-shell:
23
-
24
- .. include:: /includes/fact-mws.rst
25
-
26
- Click inside the shell to connect. Once connected, you can run the
27
- examples in the shell above.
28
-
29
- .. tabs::
30
-
31
- .. tab:: 1. Switch Database
32
- :tabid: example-1
33
-
34
- Switch Database
35
- ---------------
36
-
37
- Within the :ref:`shell <mongo-web-shell>`, ``db`` refers to
38
- your current database. Type ``db`` to display the current
39
- database.
40
-
41
- .. code-block:: javascript
42
-
43
- db
44
-
45
- The operation should return ``test``, which is the default
46
- database.
47
-
48
- To switch databases, type ``use <db>``. For example, to switch
49
- to the ``examples`` database:
50
-
51
- .. code-block:: javascript
52
-
53
- use examples
54
-
55
- You do not need to create the database before you switch.
56
- MongoDB creates the database when you first store data in that
57
- database (such as create the first collection in the database).
58
-
59
- To verify that your database is now ``examples``, type ``db`` in
60
- the :ref:`shell <mongo-web-shell>` above.
61
-
62
- .. code-block:: javascript
63
-
64
- db
65
-
66
- To create a collection in the database, see the next tab.
67
-
68
- .. tab:: 2. Insert
69
- :tabid: example-2
70
-
71
- Populate a Collection (Insert)
72
- ------------------------------
73
-
74
- MongoDB stores documents in :doc:`collections
75
- </core/databases-and-collections>`. Collections are analogous to
76
- tables in relational databases. If a collection does not exist,
77
- MongoDB creates the collection when you first store data for that
78
- collection.
79
-
80
- The following example uses the
81
- :method:`db.collection.insertMany()` method to insert new
82
- :doc:`documents </core/document>` into the ``movies``
83
- collection. You can copy and paste the example into the
84
- :ref:`shell <mongo-web-shell>` above.
85
-
86
- .. code-block:: javascript
87
-
88
- db.movies.insertMany([
89
- {
90
- title: 'Titanic',
91
- year: 1997,
92
- genres: [ 'Drama', 'Romance' ],
93
- rated: 'PG-13',
94
- languages: [ 'English', 'French', 'German', 'Swedish', 'Italian', 'Russian' ],
95
- released: ISODate("1997-12-19T00:00:00.000Z"),
96
- awards: {
97
- wins: 127,
98
- nominations: 63,
99
- text: 'Won 11 Oscars. Another 116 wins & 63 nominations.'
100
- },
101
- cast: [ 'Leonardo DiCaprio', 'Kate Winslet', 'Billy Zane', 'Kathy Bates' ],
102
- directors: [ 'James Cameron' ]
103
- },
104
- {
105
- title: 'The Dark Knight',
106
- year: 2008,
107
- genres: [ 'Action', 'Crime', 'Drama' ],
108
- rated: 'PG-13',
109
- languages: [ 'English', 'Mandarin' ],
110
- released: ISODate("2008-07-18T00:00:00.000Z"),
111
- awards: {
112
- wins: 144,
113
- nominations: 106,
114
- text: 'Won 2 Oscars. Another 142 wins & 106 nominations.'
115
- },
116
- cast: [ 'Christian Bale', 'Heath Ledger', 'Aaron Eckhart', 'Michael Caine' ],
117
- directors: [ 'Christopher Nolan' ]
118
- },
119
- {
120
- title: 'Spirited Away',
121
- year: 2001,
122
- genres: [ 'Animation', 'Adventure', 'Family' ],
123
- rated: 'PG',
124
- languages: [ 'Japanese' ],
125
- released: ISODate("2003-03-28T00:00:00.000Z"),
126
- awards: {
127
- wins: 52,
128
- nominations: 22,
129
- text: 'Won 1 Oscar. Another 51 wins & 22 nominations.'
130
- },
131
- cast: [ 'Rumi Hiiragi', 'Miyu Irino', 'Mari Natsuki', 'Takashi Naitè' ],
132
- directors: [ 'Hayao Miyazaki' ]
133
- },
134
- {
135
- title: 'Casablanca',
136
- genres: [ 'Drama', 'Romance', 'War' ],
137
- rated: 'PG',
138
- cast: [ 'Humphrey Bogart', 'Ingrid Bergman', 'Paul Henreid', 'Claude Rains' ],
139
- languages: [ 'English', 'French', 'German', 'Italian' ],
140
- released: ISODate("1943-01-23T00:00:00.000Z"),
141
- directors: [ 'Michael Curtiz' ],
142
- awards: {
143
- wins: 9,
144
- nominations: 6,
145
- text: 'Won 3 Oscars. Another 6 wins & 6 nominations.'
146
- },
147
- lastupdated: '2015-09-04 00:22:54.600000000',
148
- year: 1942
149
- }
150
- ])
151
-
152
- The operation returns a document that contains the
153
- acknowledgement indicator and an array that contains the
154
- ``_id`` of each successfully inserted documents.
155
-
156
- To verify the insert, you can query the collection (See the
157
- next tab).
158
-
159
- .. tab:: 3. Find All
160
- :tabid: example-3
161
-
162
- Select All Documents
163
- --------------------
164
-
165
- To select the documents from a collection, you can use the
166
- :method:`db.collection.find()` method. To select all documents
167
- in the collection, pass an empty document as the :ref:`query
168
- filter document <document-query-filter>` to the method.
169
-
170
- In the :ref:`shell <mongo-web-shell>`, copy and paste the
171
- following to return all documents in the ``movies``
172
- collection.
173
-
174
- .. code-block:: javascript
175
-
176
- db.movies.find( { } )
177
-
178
- .. tab:: 4. Filter Data
179
- :tabid: example-4
180
-
181
- Filter Data with Comparison Operators
182
- -------------------------------------
183
-
184
- For an equality match (``<field>`` equals ``<value>``),
185
- specify ``<field>: <value>`` in the :ref:`query filter
186
- document <document-query-filter>` and pass to the
187
- :method:`db.collection.find()` method.
188
-
189
- - In the :ref:`shell <mongo-web-shell>`, run the following
190
- query to find movies that were directed by
191
- ``Christopher Nolan``:
192
-
193
- .. code-block:: javascript
194
-
195
- db.movies.find( { "directors": "Christopher Nolan" } );
196
-
197
- You can use :ref:`comparison operators <query-selectors-comparison>`
198
- to perform more advanced queries:
199
-
200
- - Run the following query to return movies that were released
201
- before the year ``2000``:
202
-
203
- .. code-block:: javascript
204
-
205
- db.movies.find( { "released": { $lt: ISODate("2000-01-01") } } );
206
-
207
- - Run the following query to return movies that won more than
208
- ``100`` awards:
209
-
210
- .. code-block:: javascript
211
-
212
- db.movies.find( { "awards.wins": { $gt: 100 } } );
213
-
214
- - Run the following query to return movies where the
215
- ``languages`` array contains *either* ``Japanese`` or
216
- ``Mandarin``:
217
-
218
- .. code-block:: javascript
219
-
220
- db.movies.find( { "languages": { $in: [ "Japanese", "Mandarin" ] } } )
221
-
222
- .. seealso::
223
-
224
- :ref:`query-projection-operators-top`
225
-
226
- .. tab:: 5. Project Fields
227
- :tabid: example-5
228
-
229
- Specify Fields to Return (Projection)
230
- -------------------------------------
231
-
232
- To specify fields to return, pass a projection document to the
233
- :method:`db.collection.find(\<query document\>, \<projection
234
- document\>) <db.collection.find()>` method. In the projection
235
- document, specify:
236
-
237
- - ``<field>: 1`` to include a field in the returned documents
238
-
239
- - ``<field>: 0`` to exclude a field in the returned documents
240
-
241
- In the :ref:`shell <mongo-web-shell>`, run the following query to
242
- return the ``id``, ``title``, ``directors``, and ``year`` fields
243
- from all documents in the ``movies`` collection:
244
-
245
- .. code-block:: javascript
246
-
247
- db.movies.find( { }, { "title": 1, "directors": 1, "year": 1 } );
248
-
249
- You do not have to specify the ``_id`` field to return the field.
250
- It returns by default. To exclude the field, set it to ``0`` in
251
- the projection document. For example, run the following query to
252
- return only the ``title``, and the ``genres`` fields in the
253
- matching documents:
254
-
255
- .. code-block:: javascript
256
-
257
- db.movies.find( { }, { "_id": 0, "title": 1, "genres": 1 } );
258
-
259
- .. tab:: 6. Aggregate
260
- :tabid: example-6
261
-
262
- Aggregate Data (:pipeline:`$group`)
263
- -----------------------------------
264
-
265
- You can use aggregation to group values from multiple documents
266
- together and return a single result. Aggregation in MongoDB
267
- is performed with an :ref:`aggregation pipeline
268
- <aggregation-pipeline>`.
269
-
270
- While :method:`~db.collection.find()` operations are useful for
271
- data retrieval, the aggregation pipeline allows you to manipulate
272
- data, perform calculations, and write more expressive queries than
273
- simple :ref:`CRUD operations <crud>`.
274
-
275
- In the :ref:`shell <mongo-web-shell>`, run the following
276
- aggregation pipeline to count the number of occurrences
277
- of each ``genre`` value:
278
-
279
- .. code-block:: javascript
280
-
281
- db.movies.aggregate( [
282
- { $unwind: "$genres" },
283
- {
284
- $group: {
285
- _id: "$genres",
286
- genreCount: { $count: { } }
287
- }
288
- },
289
- { $sort: { "genreCount": -1 } }
290
- ] )
291
-
292
- The pipeline uses:
293
-
294
- - :pipeline:`$unwind` to output a document for each element
295
- in the ``genres`` array.
296
-
297
- - :pipeline:`$group` and the :group:`$count` accumulator
298
- to count the number of occurrences of each ``genre``. This
299
- value is stored in the ``genreCount`` field.
300
-
301
- - :pipeline:`$sort` to sort the resulting documents
302
- by the ``genreCount`` field in descending order.
303
-
304
26
Next Steps
305
27
----------
306
28
0 commit comments