@@ -140,9 +140,11 @@ In this tutorial, you will perform the following actions:
140140
141141 .. code-block:: json
142142
143- npm run dev
143+ npm run dev
144144
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.
146148
147149 .. step:: Connect to MongoDB
148150
@@ -210,36 +212,6 @@ In this tutorial, you will perform the following actions:
210212 The returned document logs to the console, including its ``_id``. Take
211213 note of this ``_id`` for use in future steps.
212214
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-
243215 Update Data
244216 ~~~~~~~~~~~~
245217
@@ -310,7 +282,7 @@ In this tutorial, you will perform the following actions:
310282 By default, Mongoose queries return thenables, which are objects with
311283 some of the properties of a JavaScript Promise. You can append the
312284 ``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
314286 guide <https://mongoosejs.com/docs/promises.html>`__ in the Mongoose
315287 documentation.
316288
@@ -397,16 +369,16 @@ In this tutorial, you will perform the following actions:
397369
398370 .. note::
399371
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()``
402374 and ``updateOne()`` by setting the ``runValidators`` option to
403375 ``true``. For more information about validators, see the `Validation
404376 <https://mongoosejs.com/docs/validation.html>`__ page in the Mongoose
405377 documentation.
406378
407379 You can use several validation methods with Mongoose. For example, you can
408380 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
410382 ``slug`` field is defined as a ``string`` with a ``minLength`` of ``4``.
411383 This means that providing a ``slug`` with fewer than 4 characters will
412384 result in a ``ValidationError``.
@@ -428,7 +400,7 @@ In this tutorial, you will perform the following actions:
428400 :start-after: start-validated-insert
429401 :end-before: end-validated-insert
430402 :language: javascript
431- :emphasize-lines: 5
403+ :emphasize-lines: 6
432404 :dedent:
433405
434406 .. tip::
@@ -439,19 +411,6 @@ In this tutorial, you will perform the following actions:
439411 For more information, see the `Validation
440412 <https://mongoosejs.com/docs/validation.html>`__ page in the Mongoose
441413 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.
455414
456415 .. step:: Introduce multiple schemas
457416
@@ -657,6 +616,36 @@ You now have a sample project that uses Mongoose to perform CRUD operations on a
657616MongoDB collection. From here, you can choose to build on this project with more
658617complex queries or document validation.
659618
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+
660649Helper Methods
661650~~~~~~~~~~~~~~
662651
0 commit comments