Skip to content

Commit eb39e54

Browse files
committed
edits
1 parent 639b47a commit eb39e54

File tree

4 files changed

+29
-19
lines changed

4 files changed

+29
-19
lines changed

source/includes/integrations/mongoose-get-started-blogSchema.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,33 @@ console.log('Created article:', article);
1919
// end-insert
2020

2121
// start-update
22+
// Updates the title of the article
2223
article.title = "The Most Awesomest Post!!";
2324
await article.save();
2425
console.log('Updated Article:', article);
2526
// end-update
2627

2728
// start-find-by-id
29+
// Finds the article by its ID. Replace <object id> with the objectId of the article.
2830
const articleFound = await Blog.findById("<object id>").exec();
2931
console.log('Found Article by ID:', articleFound);
3032
// end-find-by-id
3133

3234
// start-project-fields
35+
// Finds the article by its ID and projects only the title, slug, and content fields.
36+
// Replace <object id> with the objectId of the article.
3337
const articleProject = await Blog.findById("<object id>", "title slug content").exec();
3438
console.log('Projected Article:', articleProject);
3539
// end-project-fields
3640

3741
// start-delete-one
42+
// Deletes one article with the slug "awesome-post".
3843
const blogOne = await Blog.deleteOne({ slug: "awesome-post" });
3944
console.log('Deleted One Blog:', blogOne);
4045
// end-delete-one
4146

4247
// start-delete-many
48+
// Deletes all articles with the slug "awesome-post".
4349
const blogMany = await Blog.deleteMany({ slug: "awesome-post" });
4450
console.log('Deleted Many Blogs:', blogMany);
4551
// end-delete-many

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ import User from './model/User.js';
77
mongoose.connect("<connection string>");
88

99
// start-article-with-author
10+
// Create a new user
1011
const user = await User.create({
1112
name: 'Jess Garica',
1213
1314
});
1415

16+
// Creates a new blog post that references the user as the author
1517
const articleAuthor = await Blog.create({
1618
title: 'Awesome Post!',
1719
slug: 'Awesome-Post',
@@ -24,11 +26,13 @@ console.log('Article with Author:', articleAuthor);
2426
// end-article-with-author
2527

2628
// start-populate-author
29+
// Populates the author field with user data
2730
const articlePopulate = await Blog.findOne({ title: "Awesome Post!" }).populate("author");
2831
console.log('Article Populated:', articlePopulate);
2932
// end-populate-author
3033

3134
// start-middleware-update
35+
// Middleware to update the updated field before saving
3236
const articleUpdated = await Blog.findById("68262cac638b7ec7ed0a086b").exec();
3337
articleUpdated.title = "Updated Title";
3438
await articleUpdated.save();

source/includes/integrations/mongoose-schema-example.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ const blog = new Schema({
1717
}]
1818
});
1919
// end-schema-example
20-
const Blog = model('Blog', blogSchema);
20+
const Blog = model('Blog', blog);
2121
export default Blog;

source/integrations/mongoose-get-started.txt

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ Overview
2424
Mongoose is an Object Data Modeling (ODM) library for MongoDB. You can use
2525
Mongoose to help with data modeling, schema enforcement, model validation, and
2626
general data manipulation. Because MongoDB has a flexible data model that allows
27-
you to easily alter and update databases in the future, there aren't rigid
28-
schemas. Mongoose enforces a semi-regid schema from the beginning. When you use
29-
Mongoose, you must define a schema and model.
27+
you to alter and update databases in the future, there aren't rigid schemas.
28+
Mongoose enforces a semi-regid schema from the beginning. When you use Mongoose,
29+
you must define a schema and model.
3030

3131
In this tutorial, you will perform the following actions:
3232

@@ -38,7 +38,7 @@ In this tutorial, you will perform the following actions:
3838
- Validate your data
3939
- Use multiple schemas and middleware
4040

41-
You will also learn other useful methods to grow your experience using Mongoose with MongoDB.
41+
You will also learn additional methods to grow your experience using Mongoose with MongoDB.
4242

4343
Schemas
4444
-------
@@ -117,7 +117,7 @@ and install the necessary dependencies:
117117
npm i -D nodemon
118118

119119
Open your project in your preferred code editor. This tutorial uses ES Modules
120-
instead of CommonJS. You need to add the module type to use ES Modules. Go to
120+
instead of CommonJS. You must add the ``module`` type to use ES Modules. Go to
121121
the package.json file and add the following code:
122122

123123
.. code-block:: json
@@ -361,8 +361,8 @@ Additional Methods
361361

362362
Mongoose includes several helper methods that are abstracted from regular
363363
MongoDB methods. In this section, you can find examples of some of these
364-
methods. You don't need to add these methods to your ``index.js`` file to proceed
365-
with the tutorial, but they are helpful to reference.
364+
methods. These methods are not used specifically in this tutorial, but they are
365+
helpful to reference when getting started with Mongoose.
366366

367367
exists()
368368
~~~~~~~~
@@ -405,11 +405,11 @@ after your query, as shown in the following example:
405405
Validate Your Data
406406
------------------
407407

408-
If you refer to the schema, note that the articles inserted so far have not
409-
contained the ``author``, ``dates``, or ``comments`` fields even though these fields are
410-
included in the schema. This is because although you defined the structure of
411-
your document, you have not defined which fields are required. Without defining
412-
the required fields, you can omit any fields.
408+
The articles inserted so far have not contained the ``author``, ``dates``, or
409+
``comments`` fields, even though these fields are included in the schema. This is
410+
because although you defined the structure of your document, you have not
411+
defined which fields are required. Without defining the required fields, you can
412+
omit any fields.
413413

414414
To add data validation and define these requirements, update the schema in
415415
``Blog.js`` like the following:
@@ -429,7 +429,7 @@ its value.
429429
``value: {type: String}``.
430430

431431
You can use several validation methods with Mongoose. For example, you can set
432-
``required`` to true on any fields that you would like to require.
432+
``required`` to true on any fields that you want to require.
433433

434434
You can also validate the type and the formatting. In the preceding code, the
435435
``slug`` field is defined as a ``string`` that is always in ``lowercase``. This
@@ -448,7 +448,7 @@ Multiple Schemas
448448
----------------
449449

450450
Now that you have validation on your blog schema, and the author field is
451-
``required``, you need to update ``index.js`` to include the author. To do this,
451+
``required``, you must update ``index.js`` to include the author. To do this,
452452
you can create a separate schema.
453453

454454
In the model folder, create a new file named ``User.js``. Add the following code to
@@ -458,8 +458,8 @@ this file:
458458
:language: javascript
459459
:dedent:
460460

461-
To reference blog schema, update the ``Blog.js`` file to include the following
462-
code:
461+
To reference this new user model in the blog schema, update the ``Blog.js`` file
462+
to include the following code:
463463

464464
.. code-block:: javascript
465465

@@ -500,7 +500,7 @@ the following code to the top of the file to import the user model:
500500
:end-before: end-user-import
501501

502502
Since you added field validation to the blog schema, the previous code to
503-
insert, update, and delete blogs, as well as to specify fields to project, won't
503+
insert, update, and delete blogs, and to specify fields to project, won't
504504
pass the validation and the application will error.
505505

506506
Replace the existing code with the following code to create a new user, and then
@@ -532,7 +532,7 @@ following code:
532532
__v: 0
533533
}
534534

535-
The preceding code adds a ``users`` collection along with the ``blogs`` collection in
535+
The preceding code adds a ``users`` collection with the ``blogs`` collection in
536536
the MongoDB database. This code adds the required ``author`` field and sets its
537537
value to the ``user._id``.
538538

0 commit comments

Comments
 (0)