I have some Schemas in mongoose as follows:
var tournamentSchema = new Schema({
name: {type: String},
teams: [teamSchema],
poules: [pouleSchema],
createdAt: {type: Date, default: Date.now}
})
var pouleSchema = new Schema({
teams: [teamSchema],
createdAt: {type: Date, default: Date.now}
})
var teamSchema = new Schema({
name: {type: String},
createdAt: {type: Date, default: Date.now}
})
I will push some teams in my tournamentSchema. When ready, I want to create the poules with the teams.
Is it possible to make a relation between the 'tournamentSchema.teams' and the 'tournamentSchema.poules'? I think it is not that difficult to push teams in the poules attribute, but when I want to change a name of a team, I want to change it in the poules also. Should I do this manually or is there some relation possible?
Or is this only possible with different Models?
Thank you in advance,
Ronald.
I think mongoose population can help you. Try something like this:
var tournamentSchema = new Schema({
name: {type: String},
teams: [teamSchema],
poules: [pouleSchema],
createdAt: {type: Date, default: Date.now}
})
var pouleSchema = new Schema({
teams: [teamSchema],
createdAt: {type: Date, default: Date.now}
})
var teamSchema = new Schema({
team: {
type: mongoose.Schema.Types.ObjectId,
ref: 'teamsSchema'
}
})
var teamsSchema = new Schema({
name: {type: String},
createdAt: {type: Date, default: Date.now}
})
So every object in your teams array referencing to ObjectId in teamsSchema, and when you change name of team it will automatically affects on teams arrays. Hope it will help you.