11.. _node-mongoose-get-started:
2+ .. original URL: https://www.mongodb.com/developer/languages/javascript/getting-started-with-mongodb-and-mongoose/
23
3- =========================
4- Get Started with Mongoose
5- =========================
4+ ===================================
5+ Tutorial: Get Started with Mongoose
6+ ===================================
67
78.. facet::
89 :name: genre
@@ -210,7 +211,7 @@ Before you begin this tutorial, perform the following actions:
210211 schema and model.
211212
212213 With Mongoose, you create a schema model file for each schema that is
213- needed. First, create the a folder called ``model`` to put all your schema
214+ needed. First, create a folder called ``model`` to put all your schema
214215 files in, then create your first schema file called ``Blog.js``. Open this
215216 file and add the following code:
216217
@@ -224,11 +225,8 @@ Before you begin this tutorial, perform the following actions:
224225 inserting data into the database. The following sections show you how to
225226 perform CRUD operations using Mongoose.
226227
227- .. procedure::
228- :style: connected
229-
230228 Insert Data
231- ~~~~~~~~~~~
229+ ````````````
232230
233231 Go to ``index.js`` and add the following import statement to the top of your file:
234232
@@ -282,7 +280,7 @@ Before you begin this tutorial, perform the following actions:
282280 insert multiple articles into your database.
283281
284282 Update Data
285- ~~~~~~~~~~~
283+ ````````````
286284
287285 To update data, you can directly edit the local object with Mongoose.
288286 Then, you can use the ``save()`` method to write the update to the
@@ -315,9 +313,9 @@ Before you begin this tutorial, perform the following actions:
315313 }
316314
317315 Find Data
318- ~~~~~~~~~
316+ ``````````
319317
320- To update a specific document, you can use the Mongoose ``findById()``
318+ To find a specific document, you can use the Mongoose ``findById()``
321319 method to get a document by its ``ObjectId``.
322320
323321 Add following code to your ``index.js`` file to find a specific article by
@@ -356,9 +354,9 @@ Before you begin this tutorial, perform the following actions:
356354 documentation.
357355
358356 Specify Document Fields
359- ~~~~~~~~~~~~~~~~~~~~~~~
357+ ```````````````````````
360358
361- Like with the {+driver-short+}, you can use Mongoose to project only the
359+ You can use Mongoose to project only the
362360 fields that you need. The following code specifies to only project the
363361 ``title``, ``slug``, and ``content`` fields.
364362
@@ -386,7 +384,7 @@ Before you begin this tutorial, perform the following actions:
386384 }
387385
388386 Delete Data
389- ~~~~~~~~~~~
387+ ```````````
390388
391389 Mongoose uses the ``deleteOne()`` and ``deleteMany()`` methods to delete
392390 data from a collection. You can specify the field of the document you want
@@ -433,22 +431,12 @@ Before you begin this tutorial, perform the following actions:
433431 document, you have not defined which fields are required. Any field that
434432 is no defined as required can be omitted.
435433
436- To add data validation and define these requirements, update the schema in
437- ``Blog.js`` as shown in the following example:
438-
439- .. literalinclude:: /includes/integrations/mongoose-blogSchema-validate.js
440- :language: javascript
441- :start-after: start-blogSchema
442- :end-before: end-blogSchema
443- :dedent:
444-
445434 In Mongoose, when you include validation on a field, you must pass an
446435 object as its value.
447436
448- .. tip ::
437+ .. note ::
449438
450- When you use schemas with Mongoose, ``value: String`` is the same as
451- ``value: {type: String}``.
439+ Validators only run on the ``create()`` and ``save()`` methods.
452440
453441 You can use several validation methods with Mongoose. For example, you can
454442 set ``required`` to true on any fields that you want to require. You can
@@ -461,15 +449,35 @@ Before you begin this tutorial, perform the following actions:
461449 arrow function. This field is also specified to be impossible to change
462450 later by setting ``immutable`` to ``true``.
463451
464- .. note::
452+ To add data validation and define these requirements, update the schema in
453+ ``Blog.js`` as shown in the following example:
465454
466- Validators only run on the ``create()`` and ``save()`` methods.
455+ .. literalinclude:: /includes/integrations/mongoose-blogSchema-validate.js
456+ :language: javascript
457+ :start-after: start-blogSchema
458+ :end-before: end-blogSchema
459+ :dedent:
460+
461+ After adding this validation, your application will crash. Add an
462+ ``author`` field to the ``create()`` call in your ``index.js`` file to
463+ meet the new validation requirement:
464+
465+ .. literalinclude:: /includes/integrations/mongoose-get-started-blogSchema.js
466+ :start-after: start-validated-insert
467+ :end-before: end-validated-insert
468+ :language: javascript
469+ :emphasize-lines: 5
470+ :dedent:
471+
472+ .. tip::
473+
474+ When you use schemas with Mongoose, ``value: String`` is the same as
475+ ``value: {type: String}``.
467476
468477 .. step:: Introduce multiple schemas.
469478
470- Now that you have validation on your blog schema, and the ``author`` field is
471- ``required``, you must update ``index.js`` to include the ``author``. To do
472- this, you can create a separate schema.
479+ Next, you can add more complexity to your ``author`` field by creating a
480+ another schema, and nesting it in the blog schema.
473481
474482 In the ``model`` folder, create a new file named ``User.js``. Add the
475483 following code to this file:
@@ -478,39 +486,50 @@ Before you begin this tutorial, perform the following actions:
478486 :language: javascript
479487 :dedent:
480488
481- To reference this new user model in the blog schema, update the
482- ``Blog.js`` file to include the following code:
489+ To use your new User model to define the ``author`` field in the blog
490+ schema, update the ``Blog.js`` file with the following changes:
491+
492+ - Add ``SchemaTypes`` to the list of constructors extracted from the
493+ Mongoose library.
494+ - Change the ``author`` field ``type`` to ``SchemaTypes.ObjectId`` and add
495+ a reference (``ref``) to the ``'User'`` model.
496+
497+ The following code shows these updates:
483498
484499 .. code-block:: javascript
485500
486501 import mongoose from 'mongoose';
487502 const { Schema, SchemaTypes, model } = mongoose;
488503
489504 const blogSchema = new Schema({
490- ...
491- author: {
492- type: SchemaTypes.ObjectId,
493- ref: 'User',
494- required: true,
495- },
496- ...,
497- comments: [{
498- user: {
505+ ...
506+ author: {
499507 type: SchemaTypes.ObjectId,
500508 ref: 'User',
501509 required: true,
502510 },
503- content: String,
504- votes: Number
505- }];
511+ ...,
506512 });
507513 ...
508514
515+ You can also reuse the name User model for the ``comment.user`` field by
516+ changing the ``blogSchema`` definition:
517+
518+ .. code-block:: javascript
509519
510- The preceding code sets the ``author`` and ``comments.user`` to
511- ``SchemaTypes.ObjectId`` and adds a ``ref``, or reference, to the user
512- model. Be sure to destructure ``SchemaTypes`` from Mongoose at the top of
513- the file below the import statement.
520+ const blogSchema = new Schema({
521+ ...,
522+ comments: [{
523+ user: {
524+ type: SchemaTypes.ObjectId,
525+ ref: 'User',
526+ required: true,
527+ },
528+ content: String,
529+ votes: Number
530+ }];
531+ });
532+ ...
514533
515534 To use the new user model in your application, go to the ``index.js``
516535 file. Add the following code to the top of the file to import the user
@@ -525,8 +544,15 @@ Before you begin this tutorial, perform the following actions:
525544 insert, update, and delete blogs, and to specify fields to project, won't
526545 pass the validation and the application will error.
527546
528- Replace the existing code with the following code to create a new user,
529- and then create a new article that uses this user as the author, as shown
547+ Create a new user by adding the following ``create()`` call:
548+
549+ .. literalinclude:: /includes/integrations/mongoose-get-started-multiple-schemas.js
550+ :language: javascript
551+ :start-after: start-create-user
552+ :end-before: end-create-user
553+
554+ Update the existing ``create()`` call with the following code to create a
555+ new article that uses the new user as the author, as shown
530556 in the following code:
531557
532558 .. io-code-block::
0 commit comments