2

I want to know whether my below schema definition for PersonSchema will work or not. Is it possible to declare a schema type like my example?

I mean, I want my PersonSchema to be an Array of type EmployedPersonSchema(object) or of type UnEmployedPersonSchema(object). Can I use the OR operator like below?

const EmployedPersonSchema = new Schema({
 employeeID: String,
 taxNumber: Number
});
const UnEmployedPersonSchema = new Schema({
 personID: String;
 age: Number,
 pensionNumber: Number
});
const PersonSchema = new Schema({
 persons:{
    type: [EmployedPersonSchema || UnEmployedPersonSchema],
    default: []
 }
});

Is it the best way to do it or there are other better ways? Any help?

DATA should look like below in my MongoDB:

"persons" : [
            {
             employeeID: "001",
             taxNumber: 39765
            },
            {
             personID: "901"
             age: 68,
             pensionNumber: 2345
            }
           ]
1
  • mongoosejs.com/docs/subdocs.html -> what does arrays of subdocuments mean in their documentation ? children: [childSchema || grandChildSchema] possible to do this now??? Commented Sep 6, 2019 at 12:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.