Replace existing document #1941

Closed
scriby opened this Issue Feb 25, 2014 · 7 comments

Projects

None yet

5 participants

@scriby

I've been looking for a way to take a model instance and completely overwrite an existing entry in Mongo with it. I'd like validation and other middleware to run on the instance being saved.

The overwrite functionality added in 3.8 looks close to what I want, but it appears that it doesn't run validation and middleware.

@kiranjulapalli

Yes, I am also looking for the exact same feature. My schema have mixed sub documents and when I try to save, it only updates the changes but doesn't delete the others. In some cases, I want them to be deleted.

@vvvvalvalval

+1 I miss it badly too.

@vkarpov15

4.0 does have middleware and ability to run validators on update(). What exactly are you trying to achieve?

@vkarpov15 vkarpov15 closed this Apr 29, 2015
@kyleshrader

@vkarpov15
Update doesn't validate with options {overwrite: true, runValidators: true}

I am trying to overwrite a document, but I don't want to allow deleting required fields.

@vkarpov15

@kyleshrader can you provide some additional context in the form of code samples?

@kyleshrader

For example, I have a Model with the following schema:

{
  name: {type: String, required: true},
  someField: {type: String}
}

I expect to:

  • be able to add a new field
  • be able to remove a not-required field
  • not be able to remove a required field

To match these I am using the options:

{ overwrite: true,
runValidators: true }

using findOneAndUpdate I am not able to meet these expectations with the options above. In this case, I am able to add a new field, and remove any field (required or not).

@vkarpov15

Update validators currently don't take into account the "overwrite" option. Right now they only validate on the fields specified explicitly in the update, so if you try to $unset name it will fail, but if you do overwrite: true and send { someField: 'test' } it won't work ATM. I opened up a new issue #3556 to track this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment