So I have a schema with people and groups. I want to make it possible for a person to be related to one or more people or groups. So far I have this:
var PersonSchema = new Schema({
name : String,
});
var PersonModel = mongoose.model('Person', PersonSchema);
var GroupSchema = new Schema({
name : String,
members : [PersonSchema]
});
var GroupModel = mongoose.model('Group', GroupSchema);
How to add to PersonSchema a set of one or more groups and/or other people?
Assuming you have both
PersonsandGroupscollections in your database based on this, you could add arrays of ObjectId refs to PersonSchema to capture a person’s relationships with other people or groups. As in:Then you can use mongoose’s
populatesupport to follow those refs whenever you need their full details.