Mongoose v3 Embedded Object without Explicit Subschema Declaration

less than 1 minute read

Jump in!

var contractSchema = new mongoose.Schema({
  questions: [{
    type: {type: 'string'},
    question: 'string',
    choices: ['string'],
    cycles: 'string'
  }],
  version: String,
  state: String
});

This will allow us to have a document in the following form.

var obj = {
      questions: [
        {
          type: 'date',
          question: 'What is the start date?'
        },
        {
          type: 'date',
          question: 'What is the end date?'
        }
      ],
       version: '0.1',
       state: 'CA'
};

Alternatively, if you wanted to use subdocuments you could have done this.

var contractSchema = new mongoose.Schema({
  questions: [questionSchema],
  version: String,
  state: String
});