If I have a a Schema in Mongoose that's defined like:
var subSchema = new Schema({
some: String
});
var topSchema = new Schema({
subs: [subSchema]
});
var topModel = mongoose.model("Top", topSchema);
Is it possible to define an instance method for the sub document? I've tried the following(added before the model declaration), but it doesn't work:
subSchema.methods.someFn = function() {
return 'blah';
};
Answering my own question.
What I originally wanted to do was to create a function that can be used on the collection of subdocs, as in:
topdoc.subs.someFn();
However, what I actually did with code in the original question was create a function for a subdoc itself, as in:
topdoc.subs[i].someFn();
This works.
As far as I can tell, creating a function for the collection of subdocs is not supported by Mongoose.
I got around this by defining a method in topSchema that would do what I want.