error in documentation of mongoose. #368
-
I have noticed a small error in documentation of mongoose in the docs. Thought to mark it and added screenshot for the reference. If find important it can be removed. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
@ashupn10, thank you for starting this thread. For future reference, it's best to report issues like this one as an issue on the content repository here. The documentation here is, in fact, correct. When you look at this part of the code:
The explanation you refer to talks about the following two lines of code:
When the content refers to the main difference, it is referring to the fact that, unlike the
Looking at
The string shown is a reference to the entry in the
This, along with the To dig in a little more. When you look at the entry for a
The author collection, might look something like this:
If you wanted to show a list of books in your library and show the author's name, the data in the
After this, the data for each
I hope this helps you understand this a little better. |
Beta Was this translation helpful? Give feedback.
-
Thankyou for this reply.
…On Fri, 21 Apr, 2023, 3:10 pm Schalk Neethling, ***@***.***> wrote:
@ashupn10 <https://github.com/ashupn10>, thank you for starting this
thread. For future reference, it's best to report issues like this one as
an issue on the content repository here
<https://github.com/mdn/content/issues?q=is:issue+is:open+sort:updated-desc>.
The documentation here is, in fact, correct.
When you look at this part of the code:
const BookSchema = new Schema({
title: { type: String, required: true },
author: { type: Schema.Types.ObjectId, ref: "Author", required: true },
summary: { type: String, required: true },
isbn: { type: String, required: true },
genre: [{ type: Schema.Types.ObjectId, ref: "Genre" }],
});
The explanation you refer to talks about the following two lines of code:
author: { type: Schema.Types.ObjectId, ref: "Author", required: true },
genre: [{ type: Schema.Types.ObjectId, ref: "Genre" }],
When the content refers to the *main* difference, it is referring to the
fact that, unlike the Author model, the Book model has two fields that
are references to another model. The Author model uses "static" schema
types which contains the actual value of the model field. The Book model
uses the ObjectId schema type, which stores the id of the entry in the
referenced model.
author: { type: Schema.Types.ObjectId, ref: "Author", required: true },
Looking at author, this will look something like the following when you
inspect the Book collection (table) using a tool such as MongoDB Compass
(referenced in the article):
author: ObjectId('630a5a25786094408f11e8a1')
The string shown is a *reference* to the entry in the Author model
(collection). If you were to inspect the Author collection (table) using
Compass, you would find that there will be an entry in this collection that
has a field that resembles the following:
_id: ObjectId('630a5a25786094408f11e8a1')
This, along with the ref property, is how Mongo and a tool such as
Mongoose knows where to get the data for the author field on the Book
model when you use the populate function
<https://mongoosejs.com/docs/populate.html>.
To dig in a little more. When you look at the entry for a Book in the
collection
<https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/mongoose#book_model>,
it will show something like this.
title: "Frakenstein",
author: ObjectId('630a5a25786094408f11e8a1'),
summary: "Frankenstein; or, The Modern Prometheus is an 1818 novel written by English author Mary Shelley.",
isbn: "0395469260",
genre: ObjectId('630a5a25786094408f11e9b2'),
The author collection, might look something like this:
_id: ObjectId('630a5a25786094408f11e8a1'),
author: "Mary Shelley",
If you wanted to show a list of books in your library and show the
author's name, the data in the Book collection alone will not allow you
to do so. This then is when you will use populate (as mentioned earlier).
When you do, the database will do something like this:
Get all of the book entries for the book collection
for each of the entries
Look for any fields that is a reference field
Found `author`
Use the value of the `ref` property as the collection name and find the entry with the `ObjectId` 630a5a25786094408f11e8a1.
Replace the value of `author` with the data returned from the `Author` collection
Look for any fields that is a reference field
Found `genre`
Use the value of the `ref` property as the collection name and find the entry with the `ObjectId` 630a5a25786094408f11e9b2.
Replace the value of `genre` with the data returned from the `Genre` collection
Done.
Returns the results
After this, the data for each Book entry that is returned will look
something like this:
title: "Frakenstein",
author: {
_id: ObjectId('630a5a25786094408f11e8a1'),
author: "Mary Shelley",
},
summary: "Frankenstein; or, The Modern Prometheus is an 1818 novel written by English author Mary Shelley.",
isbn: "0395469260",
genre: {
_id: ObjectId('630a5a25786094408f11e9b2'),
genre: "Gothic novel, literary fiction, horror fiction, science fiction",
},
I hope this helps you understand this a little better.
—
Reply to this email directly, view it on GitHub
<#368 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AQLORMNY5J47RX7HHBSFIE3XCJIYRANCNFSM6AAAAAAXGMGLUY>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
@ashupn10, thank you for starting this thread. For future reference, it's best to report issues like this one as an issue on the content repository here. The documentation here is, in fact, correct.
When you look at this part of the code:
The explanation you refer to talks about the following two lines of code: