Skip to content

Commit c88384f

Browse files
committed
middleware updates
1 parent 5cc7f32 commit c88384f

File tree

2 files changed

+58
-30
lines changed

2 files changed

+58
-30
lines changed

source/includes/integrations/mongoose-get-started-multiple-schemas.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,20 @@ console.log('Article Populated:', articlePopulate);
3434
// end-populate-author
3535

3636
// start-middleware-update
37-
// Middleware to update the updated field before saving
38-
const articleUpdated = await Blog.findById("68262cac638b7ec7ed0a086b").exec();
39-
articleUpdated.title = "Updated Title";
40-
await articleUpdated.save();
41-
console.log('Article Updated:', articleUpdated);
37+
// Uses middleware to update the updated field before saving and updating
38+
39+
// Create a new article
40+
const articleMiddlewareUpdate = await Blog.create({
41+
title: 'Another Awesome Post!',
42+
slug: 'Another-Awesome-Post',
43+
author: user._id,
44+
content: 'Here is another awesome post',
45+
});
46+
console.log('Original Article: ', articleMiddlewareUpdate);
47+
// Wait
48+
await new Promise(resolve => setTimeout(resolve, 1000));
49+
// Update the article
50+
articleMiddlewareUpdate.content = "Check my updated field"
51+
await articleMiddlewareUpdate.save();
52+
console.log('Auto-updated Article:', articleMiddlewareUpdate);
4253
// end-middleware-update

source/integrations/mongoose-get-started.txt

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,14 @@ In this tutorial, you will perform the following actions:
152152
.. procedure::
153153
:style: connected
154154

155-
.. step:: Prerequisites
155+
.. step:: Verify the prerequisites.
156156

157-
Before you begin this tutorial, perform the following actions:
157+
Before you begin this tutorial, ensure you have the following components prepared:
158158

159-
- Set up a MongoDB Atlas account and configure a cluster. To view
159+
- An MongoDB Atlas account with a configured cluster. To view
160160
instructions, see the :atlas:`Get Started with Atlas </getting-started>`
161161
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.
163163

164164
.. step:: Set up your environment.
165165

@@ -626,20 +626,21 @@ In this tutorial, you will perform the following actions:
626626
In Mongoose, middleware are functions that run before, or during, the
627627
execution of asynchronous functions at the schema level.
628628

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:
634634

635635
.. literalinclude:: /includes/integrations/mongoose-blogSchema-validate.js
636636
:language: javascript
637637
:start-after: start-blogSchema-middleware
638638
:end-before: end-blogSchema-middleware
639639
:dedent:
640640

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:
643644

644645
.. io-code-block::
645646
:copyable:
@@ -653,25 +654,41 @@ In this tutorial, you will perform the following actions:
653654
:language: console
654655
:visible: false
655656

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',
660660
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,
665666
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+
}
669683

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.
672687

673688
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.
675692

676693
Next Steps
677694
----------

0 commit comments

Comments
 (0)