i was reading mongoosejs documentations and get on populate method and populate with 'ref' is some kinda obscure to understand i also saw many stackoverflow questions , MDN but nobody spent much time to explain that
var personSchema = Schema({
_id: Schema.Types.ObjectId,
name: String,
age: Number,
stories: [{ type: Schema.Types.ObjectId, ref: 'Story' }]
});
var storySchema = Schema({
author: { type: Schema.Types.ObjectId, ref: 'Person' },
title: String,
fans: [{ type: Schema.Types.ObjectId, ref: 'Person' }]
});
var Story = mongoose.model('Story', storySchema);
var Person = mongoose.model('Person', personSchema);
so here is one example and the docs are saying that ref: references which model should we use, but in this case author has ref to person and it's type is objectId and how can it store whole schema (_id, name ,age,stories) and same on stories property, how can it store whole schema (in mongoose language 'document') .
Story.
findOne({ title: 'Casino Royale' }).
populate('author').
exec(function (err, story) {
if (err) return handleError(err);
console.log('The author is %s', story.author.name);
// prints "The author is Ian Fleming"
});
here as i analysed this code finds field of title in story model then it gets author property in story model too and then gets name from second schema. as code shows,author has referenced person model but as i admited it's type is objectId and how can it store whole schema (_id, name ,age,stories)
if someone can explain this in more details they will help many of guys who did not get it like me