Skip to content

Model.update setter documentation inaccuracy / misdirection #7801

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
natanavra opened this issue May 14, 2019 · 1 comment
Closed

Model.update setter documentation inaccuracy / misdirection #7801

natanavra opened this issue May 14, 2019 · 1 comment
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Milestone

Comments

@natanavra
Copy link

natanavra commented May 14, 2019

Do you want to request a feature or report a bug?
documentation inaccuracy / misdirection

What is the current behavior?
documentation states Update will not apply setters, but setters are run (this is the Query)
https://mongoosejs.com/docs/api.html#model_Model.update

Although values are casted to their appropriate types when using update, the following are not applied:

  • defaults
  • setters
  • validators
  • middleware

If the current behavior is a bug, please provide the steps to reproduce.

const event = new Schema({
  url: {
    type: String,
    required: true,
    set: function(value) {
        this.shortUrl = value.substr(0, 100);
        return value;
    }
  },
  shortUrl: {type: String, maxlength: 100},
  count: {type: Number}
});
mongoose.model('Event', event);

const url = 'https://google.com/' + 'path'.repeat(50);
const shortUrl = url.substr(0, 100);
Event.update(
  {url}, 
  {$setOnInsert: {shortUrl}, $inc: {count: 1}}, 
  {upsert: true}
);

What is the expected behavior?
Judging from the docs, the set function should not run

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node 9.6.1
Mongoose 5.0.16 (did not update due to too many deprecation warnings)
MongoDB 4.02

NOTE
Just found this comment: #7404 (comment)

vkarpov15: the custom set will be called any time shipDate is in the filter or the update

So I'm guessing the behavior changed but not reflected in docs?

@vkarpov15 vkarpov15 added this to the 5.5.10 milestone May 18, 2019
@vkarpov15
Copy link
Collaborator

The docs are incorrect, will fix. Thanks for pointing this out.

My apologies for all the deprecation warnings. Our deprecation warnings guide should help you sort those out.

@vkarpov15 vkarpov15 added the docs This issue is due to a mistake or omission in the mongoosejs.com documentation label May 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Projects
None yet
Development

No branches or pull requests

2 participants