@@ -152,14 +152,14 @@ In this tutorial, you will perform the following actions:
152
152
.. procedure::
153
153
:style: connected
154
154
155
- .. step:: Prerequisites
155
+ .. step:: Verify the prerequisites.
156
156
157
- Before you begin this tutorial, perform the following actions :
157
+ Before you begin this tutorial, ensure you have the following components prepared :
158
158
159
- - Set up a MongoDB Atlas account and configure a cluster. To view
159
+ - An MongoDB Atlas account with a configured cluster. To view
160
160
instructions, see the :atlas:`Get Started with Atlas </getting-started>`
161
161
guide.
162
- - Install `Node.js <https://nodejs.org/en/download>`__ {+min-node-version+} or later.
162
+ - `Node.js <https://nodejs.org/en/download>`__ {+min-node-version+} or later.
163
163
164
164
.. step:: Set up your environment.
165
165
@@ -626,20 +626,21 @@ In this tutorial, you will perform the following actions:
626
626
In Mongoose, middleware are functions that run before, or during, the
627
627
execution of asynchronous functions at the schema level.
628
628
629
- To use middleware in this project, go to the ``Blog.js`` file. Here you
630
- can set the ``updated`` date field to update every time an article is
631
- saved or updated by using middleware.
632
-
633
- Add the following code to a line below your ``blogSchema `` declaration :
629
+ One example of middleware is a function that automatically updates the
630
+ ``updated`` field of a ``Blog`` instance before saving or updating.
631
+
632
+ To add this middleware function, add the following code to the
633
+ ``blogSchema`` declaration in your ``Blog.js `` file :
634
634
635
635
.. literalinclude:: /includes/integrations/mongoose-blogSchema-validate.js
636
636
:language: javascript
637
637
:start-after: start-blogSchema-middleware
638
638
:end-before: end-blogSchema-middleware
639
639
:dedent:
640
640
641
- Go to the ``index.js`` file and then add the following code to find an
642
- article, update the title, and then save the updated article:
641
+ To see the effect of this function, add the following code to your
642
+ ``index.js`` file to find an article, update the title, and then save the
643
+ updated article:
643
644
644
645
.. io-code-block::
645
646
:copyable:
@@ -653,25 +654,41 @@ In this tutorial, you will perform the following actions:
653
654
:language: console
654
655
:visible: false
655
656
656
- Article Updated: {
657
- _id: new ObjectId('...'),
658
- title: 'Updated Title',
659
- slug: 'awesome-post',
657
+ Original Article: {
658
+ title: 'Another Awesome Post!',
659
+ slug: 'another-awesome-post',
660
660
published: false,
661
- author: new ObjectId('...'),
662
- content: 'This is the best post ever',
663
- tags: [ 'featured', 'announcement' ],
664
- createdAt: 2025-05-15T18:04:28.590Z,
661
+ author: new ObjectId('683df165ce6200f8fafc9c95'),
662
+ content: 'Here is another awesome post',
663
+ tags: [],
664
+ _id: new ObjectId('683df167ce6200f8fafc9ca1'),
665
+ createdAt: 2025-06-02T18:45:59.892Z,
665
666
comments: [],
666
- __v: 0,
667
- updated: 2025-05-15T18:18:30.249Z
668
- }
667
+ updated: 2025-06-02T18:45:59.894Z,
668
+ __v: 0
669
+ }
670
+ Auto-updated Article: {
671
+ title: 'Another Awesome Post!',
672
+ slug: 'another-awesome-post',
673
+ published: false,
674
+ author: new ObjectId('683df165ce6200f8fafc9c95'),
675
+ content: 'Check my updated field',
676
+ tags: [],
677
+ _id: new ObjectId('683df167ce6200f8fafc9ca1'),
678
+ createdAt: 2025-06-02T18:45:59.892Z,
679
+ comments: [],
680
+ updated: 2025-06-02T18:46:01.034Z,
681
+ __v: 0
682
+ }
669
683
670
- When you run the application, the returned article includes an ``updated``
671
- date.
684
+ When you run the application, you can see that the original article has an
685
+ ``updated`` value, though one was not specified. You can also see that the
686
+ ``updated`` value increase when the article is modified.
672
687
673
688
Besides the ``pre()`` middleware function, Mongoose also offers a
674
- ``post()`` middleware function.
689
+ ``post()`` middleware function. For more information about middleware, see
690
+ the `Middleware <https://mongoosejs.com/docs/middleware.html>`__ page in
691
+ the Mongoose documentation.
675
692
676
693
Next Steps
677
694
----------
0 commit comments