@@ -140,9 +140,11 @@ In this tutorial, you will perform the following actions:
140
140
141
141
.. code-block:: json
142
142
143
- npm run dev
143
+ npm run dev
144
144
145
- Every time you save a code change, your application will run again.
145
+ .. note::
146
+
147
+ When you use nodemon, the code runs every time you save a file.
146
148
147
149
.. step:: Connect to MongoDB
148
150
@@ -210,36 +212,6 @@ In this tutorial, you will perform the following actions:
210
212
The returned document logs to the console, including its ``_id``. Take
211
213
note of this ``_id`` for use in future steps.
212
214
213
- To run the code, use the following command in your terminal:
214
-
215
- .. io-code-block::
216
- :copyable:
217
-
218
- .. input::
219
- :language: bash
220
-
221
- npm run dev
222
-
223
- .. output::
224
- :language: console
225
- :visible: false
226
-
227
- Created article: {
228
- title: 'Awesome Post!',
229
- slug: 'awesome-post',
230
- published: true,
231
- content: 'This is the best post ever',
232
- tags: [ 'featured', 'announcement' ],
233
- _id: new ObjectId('...'),
234
- comments: [],
235
- __v: 0
236
- }
237
-
238
- .. note::
239
-
240
- When you use nodemon, the code runs every time you save a file. Saving will
241
- insert multiple articles into your database.
242
-
243
215
Update Data
244
216
~~~~~~~~~~~~
245
217
@@ -310,7 +282,7 @@ In this tutorial, you will perform the following actions:
310
282
By default, Mongoose queries return thenables, which are objects with
311
283
some of the properties of a JavaScript Promise. You can append the
312
284
``exec()`` method to a query to receive a true JavaScript Promise. To
313
- learn more about working with promises and Mongoose, see the `Promises
285
+ learn more about working with promises in Mongoose, see the `Promises
314
286
guide <https://mongoosejs.com/docs/promises.html>`__ in the Mongoose
315
287
documentation.
316
288
@@ -397,16 +369,16 @@ In this tutorial, you will perform the following actions:
397
369
398
370
.. note::
399
371
400
- Validators automatically run on the ``create()`` and ``save()``methods.
401
- You specify them to run them on update methods, such as ``update()``
372
+ Validators automatically run on the ``create()`` and ``save()`` methods.
373
+ You can specify them to run them on update methods, such as ``update()``
402
374
and ``updateOne()`` by setting the ``runValidators`` option to
403
375
``true``. For more information about validators, see the `Validation
404
376
<https://mongoosejs.com/docs/validation.html>`__ page in the Mongoose
405
377
documentation.
406
378
407
379
You can use several validation methods with Mongoose. For example, you can
408
380
set ``required`` to true on any fields that you want to require. You can
409
- also validate the type and the formatting. In the preceding code, the
381
+ also validate the type and the formatting. In the following code, the
410
382
``slug`` field is defined as a ``string`` with a ``minLength`` of ``4``.
411
383
This means that providing a ``slug`` with fewer than 4 characters will
412
384
result in a ``ValidationError``.
@@ -428,7 +400,7 @@ In this tutorial, you will perform the following actions:
428
400
:start-after: start-validated-insert
429
401
:end-before: end-validated-insert
430
402
:language: javascript
431
- :emphasize-lines: 5
403
+ :emphasize-lines: 6
432
404
:dedent:
433
405
434
406
.. tip::
@@ -439,19 +411,6 @@ In this tutorial, you will perform the following actions:
439
411
For more information, see the `Validation
440
412
<https://mongoosejs.com/docs/validation.html>`__ page in the Mongoose
441
413
documentation.
442
-
443
- .. tip:: Mongoose Custom Setters
444
-
445
- Mongoose also provides three custom setter options that modify your data
446
- when it is saved, and can be implemented in the same was as validators:
447
-
448
- - lowercase
449
- - uppercase
450
- - trim
451
-
452
- For more information, see the `SchemaStringOptions
453
- <https://mongoosejs.com/docs/api/schemastringoptions.html#SchemaStringOptions.prototype.lowercase>`
454
- page in the Mongoose API documentation.
455
414
456
415
.. step:: Introduce multiple schemas
457
416
@@ -657,6 +616,36 @@ You now have a sample project that uses Mongoose to perform CRUD operations on a
657
616
MongoDB collection. From here, you can choose to build on this project with more
658
617
complex queries or document validation.
659
618
619
+ Mongoose Custom Setters
620
+ ~~~~~~~~~~~~~~~~~~~~~~~
621
+
622
+ Custom setters modify your data when it is saved, and can be implemented similar
623
+ to validators. Mongoose provides the following custom setters:
624
+
625
+ - ``lowercase``
626
+ - ``uppercase``
627
+ - ``trim``
628
+
629
+ The following example lowercases the characters in the ``blog.slug`` field:
630
+
631
+ .. code-block:: javascript
632
+ :emphasize-lines: 7
633
+
634
+ const blogSchema = new Schema({
635
+ ...
636
+ slug: {
637
+ type: String,
638
+ required: true,
639
+ minLength: 4,
640
+ lowercase: true,
641
+ },
642
+ ...
643
+ });
644
+
645
+ For more information, see the `SchemaStringOptions
646
+ <https://mongoosejs.com/docs/api/schemastringoptions.html#SchemaStringOptions.prototype.lowercase>`__
647
+ page in the Mongoose API documentation.
648
+
660
649
Helper Methods
661
650
~~~~~~~~~~~~~~
662
651
0 commit comments