Skip to content
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

MongoDB arrayFilters operator for PATCH operations is not allowed in the current implementation #9

Open
TheRusCompass opened this issue Feb 6, 2023 · 0 comments
Labels
needs triage This issue or pull request needs to be categorized

Comments

@TheRusCompass
Copy link
Collaborator

TheRusCompass commented Feb 6, 2023

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.

@danibix95 danibix95 added the needs triage This issue or pull request needs to be categorized label Feb 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage This issue or pull request needs to be categorized
Projects
None yet
Development

No branches or pull requests

2 participants