Automattic / Automattic/mongoose

Virtuals not called on update queries

Open
#5,763 9 comments 1 reaction 0 assignees View on GitHub
new feature
Dominant language
JavaScript
Stars
27.5k
Forks
4k
Avg merge
2d 7h
Merged PRs (30d)
35

Description

mongoose : 4.12.4
nodeJs : 8.2.1
mongoDB : 3.4.6

Hello,

Here's an enhancement purpose : currently, virtuals are not called when I use update queries like findByIdAndUpdate. You recently added a great feature : runSettersOnQuery but virtuals still not called. This because object contains update is casted before applySetters called, and fields that does not match model are removed...

I use virtual to make encryption/decryption on few fields, and it's not possible to use this very useful feature with update queries. It's possible to use save instead, but not always, especially when you have an object which contains new values...

Best regards

Contributor guide

Open the contributing guide

Research direction

Reproduce the behavior with Mongoose 4.12.4, MongoDB 3.4.6, and an update query such as findByIdAndUpdate, using runSettersOnQuery as the nearby feature to examine. Trace how the update object is cast and how setters are applied; done means virtuals run during update queries so fields used for encryption or decryption work without switching to save.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, mongodb, nodejs
Domain
backend, databases
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.