I have a mongoose diagram with a subdocument. Both the parent scheme and the child scheme have preliminary locks. For instance:
var mongoose = require('mongoose'); var Schema = mongoose.Schema; var SubSchema = new Schema( { x : Number } ); SubSchema.pre('save', function (next) { console.log("pre save Sub"); next(); }); var MainSchema = new Schema( { x : Number, children : [SubSchema] } ); MainSchema.pre('save', function (next) { console.log("pre save Main"); next(); }); var Main = mongoose.model('Main', MainSchema); var m = new Main(); m.children.push( { x : 42 } ); m.save( function(err, doc) { console.log(doc +"\n\n"); doc.children[0].x = 43; doc.save( function(err, doc2) { console.log(doc2 + "\n\n"); }); });
When I run this code, I get the following output:
pre save Sub pre save Main { __v: 0, _id: 50660b319aec895a50000002, children: [ { x: 42, _id: 50660b319aec895a50000003 } ] } pre save Main { __v: 0, _id: 50660b319aec895a50000002, children: [ { x: 43, _id: 50660b319aec895a50000003 } ] }
Any reason the pre-capture is not performed for the sub-document in the second save operation?
source share