mongoose models association

Solutions on MaxInterview for mongoose models association by the best coders in the world

showing results for - "mongoose models association"
Vincent
21 Jun 2017
1const mongoose = require('mongoose');
2const Schema = mongoose.Schema;
3
4const personSchema = Schema({
5  _id: Schema.Types.ObjectId,
6  name: String,
7  age: Number,
8  stories: [{ type: Schema.Types.ObjectId, ref: 'Story' }]
9});
10
11const storySchema = Schema({
12  author: { type: Schema.Types.ObjectId, ref: 'Person' },
13  title: String,
14  fans: [{ type: Schema.Types.ObjectId, ref: 'Person' }]
15});
16
17const Story = mongoose.model('Story', storySchema);
18const Person = mongoose.model('Person', personSchema);