-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Update validator for ObjectId fields does not parse nested objects #5744
Comments
$pull
Update validator error: CastError: Cast to ObjectID failed for value "{ '$in': [] }" at path "_id"
Thanks for the detailed repro, will investigate ASAP |
Found another bug that is related to the update validators, once again on a Steps to reproduce
My suspicion is that the update validator is checking the object passed to Illustrated in code: const mongoose = require('mongoose');
const exampleSchema = mongoose.Schema({
subdocuments: [{
name: String,
required: true
}]
});
const ExampleModel = mongoose.model('Example', exampleSchema);
const exampleDocument = {
subdocuments: [{
name: 'First'
}, {
name: 'Second'
}]
};
ExampleModel.create(exampleDocument)
.then(savedDocument => {
const firstSubDocumentId = savedDocument.subdocuments[0]._id;
ExampleModel.findByIdAndUpdate(savedDocument._id, {
$pull: {
subdocuments: {
_id: firstSubDocumentId
}
}
}, {
runValidators: true
})
.catch(error => {
// ValidationError: subdocuments: Validation failed: value: Path `name` is required.
});
}); What is the expected behavior? Stacktrace:
|
Confirmed that both scripts work with 3b4211e. Thanks for reporting, fix will be in 4.12.5, which should be released tomorrow 👍 |
Thanks for fixing this so quickly! |
Bug, mongoose 4.12.4, Node 8.7.0, MongoDB 3.4.9
Current Behavior
Update validator for ObjectId fields is not parsing
$pull
updates that include objects (example below).Steps to reproduce
$pull
update with an$in
operator (e.g.{ $pull: { field: { _id: { $in: [] } } } }
){ $in: [] }
is not a valid ObjectIdIllustrated in code:
What is the expected behavior?
$in
array are ObjectIds, not the whole object mapped to_id
.Stacktrace example:
The text was updated successfully, but these errors were encountered: