Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

217
Views
How to declare an array of different types in a Mongoose schema?

I am trying to create a Mongoose schema which contains a field that should be an array that contains different types/objects.

For example:

myArrayField: [
 { type: 'type1', type1Property: 'blabla', type1Property2: 'blabla'},
 { type: 'type2', type2Property2: 'blablo' }
]

I have taken a look at discriminators without any success and I have found mongoose.Schema.Types.Mixed. However, Mixed seems to be very open in terms that Mongo will not check anything about it. I would like to know if I can specify the different types the array can have, something like:

myArrayField: [ SchemaType1, SchemaType2]

So items can follow SchemaType1 OR SchemaType2.

Thank you in advance and regards

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Only one type can be assigned to a field. May be you should create one schema with two fields that has different types and required attributes are set to false which is default.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!