-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
findOneAndReplace throws validation errors with runValidators: false #11559
Labels
confirmed-bug
We've confirmed this is a bug in Mongoose and will fix it.
Milestone
Comments
IslandRhythms
added
the
can't reproduce
Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.
label
Mar 28, 2022
Please modify the script below to demonstrate your issue. const mongoose = require('mongoose');
const {Schema} = mongoose;
const testSchema = new Schema({
name: {
type: String,
require: true
}
});
const Test = mongoose.model('Test', testSchema);
async function run() {
await mongoose.connect('mongodb://localhost:27017');
await mongoose.connection.dropDatabase();
const entry = await Test.create({
name: 'Test'
});
await Test.findOneAndReplace({name: 'Test'}, {name: 'Testerson'}, {runValidators: false});
console.log(await Test.find());
}
run(); |
const mongoose = require('mongoose');
const {Schema} = mongoose;
const testSchema = new Schema({
name: {
type: String,
required: true // you had a typo here
}
});
const Test = mongoose.model('Test', testSchema);
async function run() {
await mongoose.connect('mongodb://localhost:27017');
await mongoose.connection.dropDatabase();
const entry = await Test.create({
name: 'Test'
});
await Test.findOneAndReplace(
{name: 'Test'},
{}, // this part is key, I am trying to replace without required fields
{runValidators: false}
);
console.log(await Test.find());
}
run(); throws
|
IslandRhythms
added
confirmed-bug
We've confirmed this is a bug in Mongoose and will fix it.
and removed
can't reproduce
Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.
labels
Mar 28, 2022
Good catch on the typo. |
It's worth pointing out that switching from findOneAndReplace to replaceOne works as expected. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do you want to request a feature or report a bug?
Report a bug. But, if it is not a bug, then request a feature.
What is the current behavior?
ValidationErrors are thrown when using the runValidators: false option in findOneAndReplace.
If the current behavior is a bug, please provide the steps to reproduce.
Have a model with required fields and try to findOneAndReplace without required fields and with runValidators set to false.
What is the expected behavior?
ValidationErrors are not thrown when using the runValidators: false option in findOneAndReplace.
Basically, we want to be able to soft delete a document by replacing it with fewer fields than is normally required without triggering validation.
I searched for findOneAndReplace + runValidators in Issues and found this. Although the issue is about
overwriteDiscriminatorKey
it did include a snippet for findOneAndReplace with runValidators, so unless it was a copy & paste error it would seem it should be supported?What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node.js: 16.13.0
Mongoose: 6.2.8
MongoDB: 4.4.6-8
The text was updated successfully, but these errors were encountered: