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

How to increment array nested object field? #6687

Open
zmras opened this issue May 15, 2020 · 8 comments · May be fixed by #9115
Open

How to increment array nested object field? #6687

zmras opened this issue May 15, 2020 · 8 comments · May be fixed by #9115
Labels
type:feature New feature or improvement of existing feature

Comments

@zmras
Copy link

zmras commented May 15, 2020

Hi! Let's assume I have the following data:

obj = {
  _id: 'someId',
  items: [
    { value: 'a', count: 5 },
    { value: 'b', count: 1 },
  ]
}

Please, I want to know how can I use obj.increment() to increment the count field of the first element in items?

I have tried obj.increment('items.0.count') but it doesn't work. I got the following error: Cannot read property '0' of undefined

@zmras zmras closed this as completed May 22, 2020
@mtrezza
Copy link
Member

mtrezza commented May 22, 2020

To clarify my previous comment: what you want to do is not documented and not supported in Parse Server, but I think you're onto something. It makes sense to support it since it is supported by MongoDB.

I had a look into why this doesn't work and I found several roadblocks:

1) Parse JS SDK:

The SDK assumes that a nested key path does not require to traverse over an array element. It fails to traverse the path, because 0 is not an object key but an array index in the example above.

https://github.com/parse-community/Parse-SDK-JS/blob/e5094aeb5c5d8b2a396f2171f9cb26a70f94a64b/src/ObjectStateMutations.js#L126-L137

This causes the error: TypeError: Cannot read property '0' of undefined.
Workaround is to fetch the object before incrementing.

2) Parse JS SDK:

For nested keys the SDK validates the existence of said key, otherwise it does not set the increment operator on that key. Effectively, this does not allow to increment a nested key for an object that has been instantiated with Parse.Object.createWithoutData(). I'm not sure why that is only checked for nested keys and what implications it has to change that.

https://github.com/parse-community/Parse-SDK-JS/blob/e5094aeb5c5d8b2a396f2171f9cb26a70f94a64b/src/ParseObject.js#L696-L703

This causes the increment instruction to be ignored without throwing an error.
Workaround is to fetch the object before incrementing.
Same root cause as for issue b) in #6455 (comment).

3) Parse Server:

SchemaController expects the Parse.Object field type to be of Object when the key to modify contains a dot:

if (fieldName.indexOf('.') > 0) {
// subdocument key (x.y) => ok if x is of type 'object'
fieldName = fieldName.split('.')[0];
type = 'Object';
}

This fails the schema validation because the field in the example above is of type Array which causes a type mismatch with the expected type Object.

This causes the error:
ParseError: 111 schema mismatch for MyClass.myField; expected Array but got Object


Conclusion:

  • I would consider roadblocks 1 and 2 to be a bug, regardless of the current issue.
  • Roadblock 3 is not a bug but needs to be changed in order to implement the feature of incrementing (or even setting?) values of nested key paths that include arrays.

@zmras
Copy link
Author

zmras commented May 23, 2020

I really thank you for the explanation. I think I will use mongodb collection.updateOne() with $inc operator directly to increment the field until the feature is considered to be implemented.

@mtrezza
Copy link
Member

mtrezza commented May 23, 2020

Do you want to reopen this issue to keep track of it?

Sent with GitHawk

@zmras
Copy link
Author

zmras commented Jun 2, 2020

Ok! I'll reopen it

@zmras zmras reopened this Jun 2, 2020
@mtrezza mtrezza added type:bug Impaired feature or lacking behavior that is likely assumed enhancement type:feature New feature or improvement of existing feature and removed type:bug Impaired feature or lacking behavior that is likely assumed enhancement type:feature New feature or improvement of existing feature labels Jul 2, 2020
@ruhci28
Copy link

ruhci28 commented Jul 16, 2020

hye, is this issue open .?

@IngridFuentes
Copy link

Is this issue still open?

@davimacedo
Copy link
Member

Yes. It is open for a PR. Would you be willed to tackle this one?

KaustubhDamania added a commit to KaustubhDamania/Parse-SDK-JS that referenced this issue Apr 13, 2021
Fixes parse-community/parse-server#6687 by
modifying the estimateAttribute function logic in src/ObjectStateMutations.js.
KaustubhDamania added a commit to KaustubhDamania/Parse-SDK-JS that referenced this issue Apr 13, 2021
Fixes parse-community/parse-server#6687 by
modifying the estimateAttribute function logic in src/ObjectStateMutations.js.
@mtrezza

This comment has been minimized.

@mtrezza mtrezza added type:feature New feature or improvement of existing feature and removed type:improvement labels Dec 6, 2021
@dplewis dplewis linked a pull request May 2, 2024 that will close this issue
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:feature New feature or improvement of existing feature
Projects
None yet
5 participants