When updating elements of primitive array using positional filtered operator, update value is incorrectly cast to array #14595
Labels
confirmed-bug
We've confirmed this is a bug in Mongoose and will fix it.
Milestone
Prerequisites
Mongoose version
7.6.2
Node.js version
20.13.1
MongoDB server version
7.0.8
Typescript version (if applicable)
5.2.2
Description
When using the filtered positional operator to update specific element(s) of an array of primitives (e.g. strings), mongoose incorrectly changes the input value from a primitive (e.g. string) to an array containing the input value.
This seems to be due to a misunderstanding of the intent of the operation. Mongoose identifies that the schema type of the path to be updated is an array, and therefore casts the input value to be an array. However, the intent of the update is to modify individual elements of that array, so the schema type for the input value should be identified as the type of the elements of the array, rather than an array itself.
This bug is not present in 7.6.1, but it appears in 7.6.2 and is still present in 7.6.11.
Steps to Reproduce
Here's an example snippet. The additional nesting might not be entirely necessary to reproduce the bug, but it's how I encountered it.
Expected Behavior
Since the intent of the update is to modify individual element(s) of the array, rather than the array itself, the schema type of the elements of the array (e.g. in this case,
String
) should be used for casting.The output of the example script should be:
// ['tag-first', 'tag-modified', 'tag-last']
The text was updated successfully, but these errors were encountered: