Skip to content

Commit c9c5b1a

Browse files
committed
Clarify usage of mongoose ref attribute
1 parent 4111e8b commit c9c5b1a

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

src/content/4/en/part4c.md

Lines changed: 1 addition & 1 deletion
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. The <i>ref</i> field suggests the name of the model that will be referred to.
172+
The field type is <i>ObjectId</i>, meaning it refers to another document. The <i>ref</i> field specifies the name of the model being referenced. Mongo does not inherently know that this is a field that references notes, the syntax is purely related to and defined by Mongoose.
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

src/content/4/fi/osa4c.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ Muistiinpanojen id:t on talletettu käyttäjien sisälle taulukkona Mongo-id:it
169169
}
170170
```
171171

172-
Kentän tyyppi on <i>ObjectId</i>, joka viittaa <i>note</i>-tyyppisiin dokumentteihin. Mongo ei itsessään tiedä mitään siitä, että kyse on kentästä, joka viittaa nimenomaan muistiinpanoihin, vaan kyseessä on puhtaasti Mongoosen syntaksi.
172+
Kentän tyyppi on <i>ObjectId</i>, eli se viittaa johonkin toiseen dokumenttiin. Kenttä <i>ref</i> määrittää sen modelin nimen, johon viitataan. Mongo ei itsessään tiedä mitään siitä, että kyse on kentästä, joka viittaa nimenomaan muistiinpanoihin, vaan kyseessä on puhtaasti Mongoosen syntaksi.
173173

174174
Laajennetaan tiedostossa <i>model/note.js</i> olevaa muistiinpanon skeemaa siten, että myös muistiinpanossa on tieto sen luoneesta käyttäjästä:
175175

@@ -493,6 +493,8 @@ usersRouter.get('/', async (request, response) => {
493493

494494
Funktion [populate](http://mongoosejs.com/docs/populate.html) kutsu siis ketjutetaan kyselyä vastaavan metodikutsun (tässä tapauksessa <i>find</i>) perään. Populaten parametri määrittelee, että <i>user</i>-dokumenttien <i>notes</i>-kentässä olevat <i>note</i>-olioihin viittaavat <i>id</i>:t korvataan niitä vastaavilla dokumenteilla.
495495

496+
Mongoose tekee ensin kyselyn <i>users</i>-kokoelmaan käyttäjien hakemiseksi ja sen jälkeen kyselyn <i>notes</i>-kokoelmaan muistiinpanojen hakemiseksi. Mongoose osaa tehdä jälkimmäisen kyselyn oikeaan kokoelmaan, koska määrittelimme aiemmin <i>user</i>-skeemassa <i>notes</i>-kentälle <i>ref</i>-attribuutin, joka kertoo Mongooselle, mihin kokelmaan <i>notes</i>-kentässä viitataan.
497+
496498
Lopputulos on jo melkein haluamamme kaltainen:
497499

498500
![Selain renderöi osoitteessa localhost:3001/api/users taulukollisen JSON:eja joilla kentät username, name, id ja notes. Kenttä notes on nyt olio jolla on kentät content, important, id ja user](../../images/4/13new.png)

0 commit comments

Comments
 (0)