type: mongoose.Schema.Types.ObjectId, ref: 'User' code example
Example 1: mongoose populate
Story.
findOne({ title: /casino royale/i }).
populate('author', 'name').
exec(function (err, story) {
if (err) return handleError(err);
console.log('The author is %s', story.author.name);
console.log('The authors age is %s', story.author.age);
});
Example 2: mongoose referenced schema
const author = new Person({
_id: new mongoose.Types.ObjectId(),
name: 'Ian Fleming',
age: 50
});
author.save(function (err) {
if (err) return handleError(err);
const story1 = new Story({
title: 'Casino Royale',
author: author._id
});
story1.save(function (err) {
if (err) return handleError(err);
});
});