Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to have an array of alternative subdocument schemas in mongoose
    text
    copied!<p><strong>Is it possible to have an array of alternative subdocument schemas in mongoose?</strong></p> <p>For instance, take this made up scenario: if i have a main schema for a person and i have a field called vehicles of type array, i want to be able to define sub document schemas, but for each vehicle type (car, motorbike, bike, bus etc etc ) :</p> <pre><code>var car = new mongoose.Schema({ make: { type: String, required: true, default: 'Ford', trim: true }, model: { type: String, trim: true }, wheels: Number, }); var motorbike = new mongoose.Schema({ make: { type: String, required: true, default: 'Honda', trim: true }, model: { type: String, trim: true }, seats: Number, }); var bike = new mongoose.Schema({ make: { type: String, required: true, default: 'MuddyFox', trim: true }, model: { type: String, trim: true }, lights: Number, }); var bus = new mongoose.Schema({ make: { type: String, required: true, default: 'MAN', trim: true }, model: { type: String, trim: true }, dents: Number, }); /* person */ var person = new mongoose.Schema({ name: { type: String, required: true, default: 'Joe Bloggs', trim: true }, timeCreated: { type: Date, required: true, default: Date.now }, vehicles: [car, motorbike, bike, bus] /* this is what i want to do */ }); db.model('person', person); </code></pre> <p>Note the person schema. </p> <p>So that person can have any of those vehicles, but the data for each vehicle is still validated against it's own schema.</p> <p>Thanks in advance</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload