Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

does mongoose have an isDirty check?

does mongoose have an isDirty check?

Problem

I have a Mongoose setup which involves an embedded-schema, lets say: A Blogpost with embedded comments. Comments can be edited by the original publisher as well as by an editor/admin. After adding / editing a comment the entire blogpost is saved.

I have some custom mongoose's 'pre' middleware set up on the embedded comment-schema which automatically sets the lasteditdate for that particular comment.

The thing is that 'pre' is called on EVERY comment on the blogpost, since I call save() on the blogpost. (For other reasons I need to do it like this) . Therefore, I need a way to check which comments have changed (or are new) since they were last saved (as part of the Blogpost overall save())

The questio: how to check in 'pre' whether a comment has changed or not? Obviously calling this.isNew isn't sufficient, since comments could be edited (i.e: aren't new) as well.

Is there any isDirty or similar that I'm overlooking?

Problem courtesy of: Geert-Jan

Solution

You may use the modified getter:

if (doc.modified) {
   // :)
}
Solution courtesy of: aaronheckmann

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

does mongoose have an isDirty check?

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×