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 use condition expressions in a query? #608

Open
nbulaj opened this issue Dec 16, 2022 · 6 comments
Open

How to use condition expressions in a query? #608

nbulaj opened this issue Dec 16, 2022 · 6 comments

Comments

@nbulaj
Copy link

nbulaj commented Dec 16, 2022

Hey. I have a question about using Condition expressions with Dynamoid.
See https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ConditionExpressions.html

I have a users table with name: String and settings: Map attributes. Settings could contain a lot of different nested maps. I need to filter my query and find only users which has color setting in settings map, something like:

FilterExpression = 'attribute_exists(#0.#1)',
ExpressionAttributeNames = {
    "#0": "settings",
    "#1": "color"
},

Is it possible to add such condition to Dynamoid where query on using some plain AWS sdk call? Thanks 🙇

@andrykonchin
Copy link
Member

I will check if it's possible to do it with QueryFilter/ScanFilter request parameters (that are deprecated now).

@nbulaj
Copy link
Author

nbulaj commented Nov 28, 2023

Sorry for bothering @andrykonchin , but maybe any update on this? Found filter_expression_convertor (

"attribute_exists (#{name_or_placeholder})"
) don't know if it's possible to reuse it for my case.

@andrykonchin
Copy link
Member

andrykonchin commented Nov 29, 2023

It seems QueryFilter/ScanFilter don't support conditions on collections like List and Map:

This parameter does not support attributes of type List or Map.
https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Scan.html#DDB-Scan-request-ScanFilter
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/LegacyConditionalParameters.QueryFilter.html

But recently where was switched from them to the Expressions style, that does support such conditions.

So right now 'settings.color.not_null' => true should not work but it seems easy to support it. Will try to add it soon and release in the next release.

@nbulaj
Copy link
Author

nbulaj commented Feb 26, 2024

Hi @andrykonchin . I see new version is here, I checked https://github.com/Dynamoid/dynamoid/pull/655/files but not sure how correctly to pass params to the .where method to filter by the Map fields which has some key 😞 Can you please help with that? Maybe we also need some READE section with how-to section on how to use new expressions

I still see:

field :metadata, :map 

# ...

Model.where("metadata.scores.not_null": true ).count

Traceback (most recent call last):
        1: from (irb):9
Dynamoid::Errors::Error (Unsupported operator screen_scores in metadata.scores.not_null)

@nbulaj
Copy link
Author

nbulaj commented Feb 26, 2024

OK I see Dynamoid::Criteria::Chain doesn't allow it via def field_condition which does the following:

name, operator = key.to_s.split('.')

So for where("settings.color.not_null": true) it returns => ["metadata", "screen_scores", "not_null"]

@andrykonchin
Copy link
Member

The work is in progress (#696) - I will come back to it soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants