Story
.findOne({ title: "Bob goes sledding" })
.populate("author")
.exec(function (err, story) {
if (err) return handleError(err);
console.log("The author is %s", story.author.name);
// prints "The author is Bob Smith"
});
here we already know that there is only one author in a book, so this is not the way to write
[{type: Schema.Types.ObjectId, ref: "Author"}]
var storySchema = Schema({
author : { type: Schema.Types.ObjectId, ref: "Author" },
title : String
});
suppose there are multiple authors in a book first, and change schema to
.var storySchema = Schema({
author : [{ type: Schema.Types.ObjectId, ref: "Author" }],
title : String
});
so when I execute the following code, is the story.author in the return function an array containing the author?
Story
.findOne({ title: "Bob goes sledding" })
.populate("author")
.exec(function (err, story) {
if (err) return handleError(err);
console.log("The author is %s", story.author.name);
// prints "The author is Bob Smith"
});