Tuesday, 27 August 2013

How do I set a setter for a subdocument in Mongoose?

How do I set a setter for a subdocument in Mongoose?

Subdocuments (embedded documents) in Mongoose can be specified in a schema
like:
var childSchema = new Schema({ name: 'string' });
var parentSchema = new Schema({
children: [childSchema]
});
But how do I specify setters on the collection and require that the array
not be empty when the type is an embedded schema? This code will throw an
errors that the embedded schema is not a valid type:
function someSetter = function(val) {
// Do something on set
return val;
};
var parentSchema = new Schema({
children: [{type: childSchema, set: someSetter, required: true}]
});

No comments:

Post a Comment