-
-
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
Bug in required field validation in findOneAndReplace #13715
Comments
I found the the error started to happen in v7.3.2 |
It seems to be only affected by the When you have the properties directly in the update object without |
Will try to find the time to create a script for reproduction |
const mongoose = require('mongoose');
const testSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
age: {
type: String,
required: true
}
});
const Test = mongoose.model('Test', testSchema);
async function run() {
await mongoose.connect('mongodb://localhost:27017');
await mongoose.connection.dropDatabase();
await Test.create({
name: 'Test Testerson',
age: 100
});
await Test.findOneAndUpdate({}, { $set: { name: 'Test' } }, { new: true, runValidators: true, overwrite: true })
}
run(); |
Thanks @IslandRhythms Did not came around to it. |
@TarSzator this is expected behavior. Without That's because Mongoose strict mode filters out unknown properties in the replacement doc, and So long story short, if you're using |
Thats only half the issue!
So either the documentation needs updates or it is a bug @vkarpov15 Why are you so fast in closing a ticket instead of asking the author for if this solves the issue and if he does not response for 2 days you close the ticket? |
@TarSzator I'm sorry, you make a fair point that our documentation is out of date. In older versions of Mongoose and MongoDB (Mongoose 5 and older), |
Since the ticket is now marked a "doc" (Thanks for that 😃 ) I would like to add another thing. Maybe I overlooked it, but when |
What do you mean by "is the query merged into |
When you check the documentation here, it is stated that |
That merging happens on the MongoDB server. |
Ohhh sorry. Did not know that this is controlled via MongoDB and not Mongoose. |
docs(model): replace outdated docs on deprecated `findOneAndUpdate()` `overwrite` option
Thanks @vkarpov15 for the work you put into this |
Prerequisites
Mongoose version
7.4.2
Node.js version
18.17.0
MongoDB server version
5.0
Typescript version (if applicable)
5.1.6
Description
We use
findOneAndUpdate
with theoverwrite
option set to true (so equal tofindOneAndReplace
) to update.We also set the
new
andrunValidators
option to true.Then in our update block we use
$set
and$push
in some cases also$setOnInsert
to modify the entry.Sadly since the update to mongoose
v.7.4.1
and also after the update tov7.4.2
we get errors that required fields are not set.In all cases this is not true. Either because the fields are in the the entry already and are not touched by the update at all, nor in the
$setOnInsert
cases because the fields are in the filter object. In these last cases also additionally adding them to the$set
portion of the update object did not help.As far as I understand the documentation the required validators should not be executed in these cases at all.
I like the idea that they are but I understand that this is not easy to achieve and the bug shows that the current way does not work and was probably not intended.
Steps to Reproduce
Sadly I can not share the company code but it is easy to reproduce by creating a model with some required fields.
Then add an entry to the collection.
Then try to update it with the options and update objects described in the Description of this issue.
Expected Behavior
No required fields errors in the named cases mentioned in the Description
The text was updated successfully, but these errors were encountered: