Skip to content

Commit c75bb79

Browse files
Update part4c.md
Added information about the "ref" property referring to the model name of the other collection. Also added a one-liner on mongoose's under-the-hood operation for populate method.
1 parent 4a4c61b commit c75bb79

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/content/4/en/part4c.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ The ids of the notes are stored within the user document as an array of Mongo id
169169
}
170170
```
171171

172-
The type of the field is <i>ObjectId</i> that references <i>note</i>-style documents. Mongo does not inherently know that this is a field that references notes, the syntax is purely related to and defined by Mongoose.
172+
The type of the field is <i>ObjectId</i> that references <i>note</i>-style documents. Mongo does not inherently know that this is a field that references notes, the syntax is purely related to and defined by Mongoose. The <i>ref</i> field suggests the name of the model that will be referred to.
173173

174174
Let's expand the schema of the note defined in the <i>models/note.js</i> file so that the note contains information about the user who created it:
175175

@@ -491,7 +491,7 @@ usersRouter.get('/', async (request, response) => {
491491
})
492492
```
493493

494-
The [populate](http://mongoosejs.com/docs/populate.html) method is chained after the <i>find</i> method making the initial query. The argument given to the populate method defines that the <i>ids</i> referencing <i>note</i> objects in the <i>notes</i> field of the <i>user</i> document will be replaced by the referenced <i>note</i> documents.
494+
The [populate](http://mongoosejs.com/docs/populate.html) method is chained after the <i>find</i> method making the initial query. The argument given to the populate method defines that the <i>ids</i> referencing <i>note</i> objects in the <i>notes</i> field of the <i>user</i> document will be replaced by the referenced <i>note</i> documents. Mongoose first queries the <i>users</i> collection for the list of users, and then queries the collection corresponding to the model object specified by the <i>ref</i> property in the users schema for data with the given object id.
495495

496496
The result is almost exactly what we wanted:
497497

0 commit comments

Comments
 (0)