schema.pre('save') not triggered by MyModel.update()
See original GitHub issueexample.js:
var mongoose = require('mongoose')
, Schema = mongoose.Schema
mongoose.connect('mongodb://localhost/test')
var Thing = new Schema({
title : String,
content : String
})
Thing.pre('save', function(next){
console.log("saving: %s (%s)", this.title, this.content)
next()
})
var MyThing = mongoose.model('Thing', Thing)
var example = new MyThing({
title : 'foo',
content : 'bar'
})
example.save(function(err){
MyThing.update({title:'foo'},{content:'baz'}, function(err, rs){
console.log(rs)
})
})
Output:
$ node example.js
saving: foo (bar)
1
The pre is triggered by the initial save, but not the update.
Issue Analytics
- State:
- Created 12 years ago
- Comments:11 (2 by maintainers)
Top Results From Across the Web
Mongoose .pre('save') does not trigger - node.js - Stack Overflow
Use pre('validate') instead of pre('save') to set the value for the required field. Mongoose validates documents before saving, ...
Read more >Mongoose v6.8.1: Middleware
Calling pre() or post() after compiling a model does not work in Mongoose in general. For example, the below pre('save') middleware will not...
Read more >Mongoose Pre-Save Hooks (144) - YouTube
DONATE TO THE SHOW!!!Donate any amount - https://bit.ly/2XmweLcMusic commonly used on this channel:http://birocratic.com ...
Read more >Mongoose Middleware | The Javascript
const schema = Schema({ name: String }); schema.pre('save', function() { doc === this; // true }); const Model = mongoose.model('Test', ...
Read more >Mongoose Middleware v4.4.14
Pre and post save() hooks are not executed on update() , findOneAndUpdate() , etc. You can see a more detailed discussion why in...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is fucking bullshit. we can’t do anything async in setters. idiots.
This is not a bug. pre save is a hook that fires on instances when their save method is called, not on the model when update is called.
Model.update let’s you send atomic $set operations directly to mongodb.