Check if a path was set but not modified

93 views
Skip to first unread message

Jason Cust

unread,
Aug 14, 2015, 3:53:53 PM8/14/15
to Mongoose Node.JS ODM
Since `document.set` will only update a value if it is a value not equal to the current value (see: https://github.com/Automattic/mongoose/blob/53d91e7ef863f35a0a4907f62b9973d23729172d/lib/document.js#L643), is there a way to inquire whether an attempt was made to set the value?

Use case: a modified by field that stores a user objectID. The same user might make two consecutive updates and therefore the field would not be marked as modified. A validation check to see if that field is modified would therefore fail.

Essentially is there a way to indicate it was modified but without a value change (e.g. modified to the same value)?

Ryan Wheale

unread,
Aug 14, 2015, 4:20:01 PM8/14/15
to Mongoose Node.JS ODM
Seems to me like you could make a plugin which does the following:

loops over all the paths for a schema
adds a "setter" function to each path
mark the field as modified every time the setter is called - something like doc.markModified('path_name'))
you can add other properties to the document to keep track of "modified but not changed" paths.

~Ryan

--
Documentation - http://mongoosejs.com/
Plugins - http://plugins.mongoosejs.com/
Bug Reports - http://github.com/learnboost/mongoose
Production Examples - http://mongoosejs.tumblr.com/
StackOverflow - http://stackoverflow.com/questions/tagged/mongoose
Google Groups - https://groups.google.com/forum/?fromgroups#!forum/mongoose-orm
Twitter - https://twitter.com/mongoosejs
IRC - #mongoosejs
---
You received this message because you are subscribed to the Google Groups "Mongoose Node.JS ODM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-orm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jason Cust

unread,
Aug 15, 2015, 10:27:16 AM8/15/15
to Mongoose Node.JS ODM
Thanks Ryan. I was hoping to avoid something of that nature and was wondering if there was a "dirty" flag or similar to indicate a field had been touched and "modified" was if the value changed. I've walked the code a few times and I couldn't find anything like that but was hoping I just glossed over it.
Reply all
Reply to author
Forward
0 new messages