pre update mongoose hook options ignored
Last active
October 14, 2018 23:44
-
-
Save YuriGor/cc8114a0d0b1ad151eaf58baab24aa3a to your computer and use it in GitHub Desktop.
pre update mongoose hook options ignored - https://github.com/Automattic/mongoose/issues/7133
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ node ./pre-update.js | |
mondo connected | |
after mongoose.connect | |
pre-updateOne defined with default(?) options | |
pre-updateOne defined by regex with default(?) options | |
pre-updateOne defined with document/query flags | |
pre-updateOne defined with document flag | |
pre-updateOne defined with query flag | |
pre-updateOne defined with no flags | |
(node:14470) DeprecationWarning: collection.remove is deprecated. Use deleteOne, deleteMany, or bulkWrite instead. | |
I'll be updated soon 5bc3d2e4822e7f38868ad45a | |
----------------- | |
default preupdate! | |
----------------- | |
default regex preupdate! | |
----------------- | |
document/query preupdate! | |
----------------- | |
document preupdate! | |
----------------- | |
query preupdate! | |
----------------- | |
no flags preupdate! | |
I was updated! 5bc3d2e4822e7f38868ad45a |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mongoose = require('mongoose'); | |
mongoose.Promise = global.Promise; | |
function hiFromPre(name, me) { | |
console.debug('-----------------'); | |
console.debug(`${name} preupdate!`); | |
} | |
async function main() { | |
const mongoUri = 'mongodb://localhost/pre'; | |
await mongoose.connect( | |
mongoUri, | |
{ useNewUrlParser: true }, | |
error => { | |
if (error) console.error(error); | |
else console.info('mondo connected'); | |
}, | |
); | |
console.debug('after mongoose.connect'); | |
const schema = new mongoose.Schema({ name: { type: String } }); | |
console.debug('pre-updateOne defined with default(?) options'); | |
schema.pre('updateOne', async function() { | |
hiFromPre('default', this); | |
}); | |
// does regex work? | |
console.debug('pre-updateOne defined by regex with default(?) options'); | |
schema.pre(/updateOne/, async function() { | |
hiFromPre('default regex', this); | |
}); | |
console.debug('pre-updateOne defined with document/query flags'); | |
schema.pre('updateOne', { document: true, query: true }, async function() { | |
hiFromPre('document/query', this); | |
}); | |
console.debug('pre-updateOne defined with document flag'); | |
schema.pre('updateOne', { document: true, query: false }, async function() { | |
hiFromPre('document', this); | |
}); | |
console.debug('pre-updateOne defined with query flag'); | |
schema.pre('updateOne', { document: false, query: true }, async function() { | |
hiFromPre('query', this); | |
}); | |
console.debug('pre-updateOne defined with no flags'); | |
schema.pre('updateOne', { document: false, query: false }, async function() { | |
hiFromPre('no flags', this); | |
}); | |
const Model = mongoose.model('Model', schema); | |
await Model.remove({}); | |
let res = await Model.create({ | |
name: "I'll be updated soon", | |
}); | |
console.log(res.name, res._id); | |
await Model.updateOne( | |
({ _id: res._id }, { $set: { name: 'I was updated!' } }), | |
); | |
const updRes = await Model.findById(res._id); | |
console.debug(updRes.name, updRes._id); | |
await mongoose.disconnect(); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment