crud-service
crud-service copied to clipboard
MongoDB arrayFilters operator for PATCH operations is not allowed in the current implementation
This requirement comes from the following design: we needed a collection with a property composed of nested arrays of objects, acting as a sort of matrix (rows with the first level array, columns with the second level array). Something like this:
{
firstLevelArrayOfObjects: {
type: 'array',
items: {
type: 'object',
properties:
secondLevelArrayOfObjects: {
type: 'array',
items: {
type: 'object',
properties: { ... },
},
},
},
},
},
}
But most importantly, we needed to perform PATCH operations only on specific elements of the second level array.
According to the MongoDB documentation it seems to be possible to update a specific element of an array using the arrayFilters
operator of the findOneAndUpdate
method. However in the patchById
method which calls the findOneAndUpdate
method we are currently not supporting such parameter. Note that this should also be possible for bulk operations, see the updateMany on the MongoDB documentation. Thus we should also modify the patchMany
method.
Please note that I'm not 100% sure that this arrayfilters
operator is the only way to update a specific element of a nested array, maybe this is also possible with the document filter, the difference between the two are not clear to me yet. I think that this operator might be useful when we want to update all the nested array elements that matches some precise conditions (e.g. the array elements with a given property which is $gte
a given value).
P.S. For further info, note that while updates using the positional operator are not possible in nested arrays, they should be possible using a filtered positional operator.