1Story.
2 findOne({ title: 'Casino Royale' }).
3 populate('author').
4 exec(function (err, story) {
5 if (err) return handleError(err);
6 console.log('The author is %s', story.author.name);
7 // prints "The author is Ian Fleming"
8 });
1const storySchema = Schema({
2 authors: [{ type: Schema.Types.ObjectId, ref: 'Person' }],
3 title: String
4});
5
6// Later
7
8const story = await Story.findOne({ title: 'Casino Royale' }).populate('authors');
9story.authors; // `[]`
1var mongoose = require('mongoose')
2 , Schema = mongoose.Schema
3
4var eventSchema = Schema({
5 title : String,
6 location : String,
7 startDate : Date,
8 endDate : Date
9});
10
11var personSchema = Schema({
12 firstname: String,
13 lastname: String,
14 email: String,
15 gender: {type: String, enum: ["Male", "Female"]}
16 dob: Date,
17 city: String,
18 interests: [interestsSchema],
19 eventsAttended: [{ type: Schema.Types.ObjectId, ref: 'Event' }]
20});
21
22var Event = mongoose.model('Event', eventSchema);
23var Person = mongoose.model('Person', personSchema);
24
1var mongoose = require('mongoose')
2 , Schema = mongoose.Schema
3
4var eventSchema = Schema({
5 title : String,
6 location : String,
7 startDate : Date,
8 endDate : Date
9});
10
11var personSchema = Schema({
12 firstname: String,
13 lastname: String,
14 email: String,
15 gender: {type: String, enum: ["Male", "Female"]}
16 dob: Date,
17 city: String,
18 interests: [interestsSchema],
19 eventsAttended: [{ type: Schema.Types.ObjectId, ref: 'Event' }]
20});
21
22var Event = mongoose.model('Event', eventSchema);
23var Person = mongoose.model('Person', personSchema);