0

I have the following scheme:

var user = Schema({
  id: Number,
  name: String,
  surname: String,
  role: { type: Schema.Types.ObjectId, ref: "" }//member or crew
  property: Number
});

var member = Schema({
  cod_id: Number,
  aa: String,
  bb: String,
});

var crew = Schema({
  cod_id: Number,
  cc: String,
  dd: String,
});

Member and crew, they are both users but they have different attributes.

The only attributes that are equal are: name, surname, role and property.

What I would like to understand if it were possible to do such a thing, specifying in user the role attribute that can be either member or crew, refer to the specific model in question.

Everything stems from the need to have the property attribute in a single model and not having to put this attribute in either member or crew, otherwise when I have to do a search I have to do two, one in the model member and one in the crew, waiting for don't have duplicate problems.

Can you give me some advice?

Paul
  • 3,644
  • 9
  • 47
  • 113
  • Is your role in user is an array which can have both reference to both member & crew or just an object which can have either member or crew (any one for a given user document)? – whoami - fakeFaceTrueSoul Sep 11 '19 at 02:05
  • @srinivasy: pardon, it's not an array I corrected. if I understand correctly what you are asking must refer to both the member and the crew, then the first. I've seen this, but I'm not sure that's what I'm looking for: https://mongoosejs.com/docs/populate.html#dynamic-ref – Paul Sep 11 '19 at 13:07
  • Yes I'm referring to refPath, that's what I've used last time for this scenario, maybe you can look into this as well : https://stackoverflow.com/questions/35468855/multiple-schema-references-in-single-schema-array-mongoose – whoami - fakeFaceTrueSoul Sep 11 '19 at 15:21

0 Answers0