Using ProjectionExpression on List that contain maps - amazon-dynamodb

Say that this is the table struct:
[{ name:"test", age:99,
Info: [
{ location:"A", num:11 },
{ location:"B", num:99 }
]
}]
What i want to get is something like this:
{ name: "test",
Info:[
{location:"A"},
{location:"B"}
]}
would that be possible? I can't seem to make it work unless I specify the index.
ProjectionExpression="name, #mp[0].location",
Select='SPECIFIC_ATTRIBUTES',
ExpressionAttributeNames={"#mp": "Info"}
How do I do this?

Based on the documentation you can either specify the whole object or with index.
http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.Attributes.html#Expressions.Attributes.NestedAttributes
Working as documented.
Hope it helps.

It's a little tricky if you do not know the length of list. You could try using a max length for the list and use something like (assuming 3 is the max length)
ProjectionExpression="#mp[0].location,#mp[1].location,#mp[2].location"

Related

Move an item within a DynamoDB list

I have a list in a DynamoDB table and would like to move items to different positions in the same list, is there a way to do this in a single update?
At the moment, I'm looking at having to read the list, modify it, then write it back again, but would prefer doing it all in a single update, is there a way to do this?
Edit to add example
So here's some noddy data that shows what I'd like to do:
If the data started like this:
Item: { COLUMN: [ "Element_0", "Element_1", "Element_2", "Element_3" ] }
Then I'd give it from and to indices and it would move the element. So for example if I gave it a from index of 0 and to index of 2 the data should end up like this:
Item: { COLUMN: [ "Element_1", "Element_2", "Element_0", "Element_3" ] }
You can do this with an Update Expression, but it's a little tricky, since you don't have the data.
Basically, you have to create a dynamic update statement that sets every value you want to move. Something like this works:
aws dynamodb update-item --table-name test --key '{"pk":{"S":"1"}}' --update-expression "SET #list[1] = #list[2], #list[2] = #list[1]" --region us-west-2 --profile jw-test --expression-attribute-names '{"#list": "list"}'
I created a table with a key of pk, with a value of 1. The list before the update was like this:
[
'one',
'two',
'three',
'four'
]
After the update it looks like this:
[
'one',
'three',
'two',
'four'
]
Default answer if this isn't possible in a single update.
Read out the list, modify it, then write it back. It's not elegant, but it works and isn't that ugly either.
It's not atomic though so any answer that can do it in a single update will get the check mark.

JSONPath to get parameter from an array

I have a response body I believe is an array it looks like this:
{"id":982,"form_stage":"1","customer_hash":"fffae165253f494a95bdb753ca87716c"}
In this case I want to get "customer_hash" and I have tried every combination of $..customer_hash $.customer_hash, etc but none of them work because this doesn't start with "response" or "result" I guess it is an array not an object(?).
Can anyone tell me what would be the JSONPath mapping to get the parameter "customer_hash"?
Try using:
$.customer_hash
Output:
[
"fffae165253f494a95bdb753ca87716c"
]

Firebase startAt String only takes first character

I have a structure like below under xyz
{
"pushKey000": {
"findKey": "john_1",
"userName": "john",
"topic": 1
},
"pushKey001": {
"findKey": "john_2",
"userName": "john",
"topic": 2
},
"pushKey002": {
"findKey": "joel_1",
"userName": "joel",
"topic": 1
}
}
Now am trying to make a query where I want data of all entries with findKey starting with "john". I tried the following:(Using REST for example)
https://abc.firebaseio.com/xyz.json?orderBy="findKey"&startAt="john"
This gives me all the results including 'joel'. Basically it just uses the first character of startAt, in this case J.
This firebase video fires the same type of query but only searches with just first character.
Is there something wrong that I am doing or is there is any other way to retrieve it using findKey? Thanks a lot for the help in advance
PS: My .indexOn is on findKey and can't change it
There is nothing wrong with your code, there is something wrong with your expectations. (I always wanted to write that as an answer :))
The startAt() function works as a starting point for your query, not a filter. So in your case it will find the first occurance of "john" and return everything from that point forward (Including Joel, Kevin, Tim, etc...).
Unfortunatly there is no direct way to do a query where findKey contains the string "john". But luckely there is a (partial) workaround using endAt().
You query will look like this:
orderBy="findKey"&startAt="john"&endAt="john\uf8ff"
Here \uf8ff is the last unicode character (please correct me if I'm wrong).
With this you can query for values that start with "john" like "johnnie", "johnn", "john". But not "1john" or "johm" or "joel".

Adding values to the keys in Map

I have a record
record = [ [ name1:'value1', name2:'value2', name3:'value3' ],
[ name1:'value6', name2:'value7', name3:'value8' ] ]
I would like to add two more key/value pairs to with values as boolean(true/false) as below
record = [ [ name1:'value1', name2:'value2', name3:'value3', name4:false, name5:true ],
[ name1:'value6', name2:'value7', name3:'value8', name4:false, name5:true ] ]
When I tried to use add or put functions, doesnt seem to work(either replacing the existing values or not doing anything)
Just do:
record = record.collect { it + [ name4:false, name5:true ] }
Or you can also do:
record = record*.plus( name4:false, name5:true )
To add to Patricks answer above (+1), a Map contains a set not a list so all keys must be unique. So you cannot assign multiple values to a single key directly.
Among many solutions, you can alternatively, save an object:
Map<String, myObject>
that holds many different values and this will still maintain the uniqueness of the set since there will only be one key.

Flex 3: Passing Array to Custom Component

I created a custom button component that accepts an array as a property.
I set the property as follows:
titleDims="[{Month: comboBox1.text, Year:comboBox2.text, Sales Order:comboBox3.text}]"
and I get the following error:
"1084: Syntax error: expecting rightparen before colon."
Wat is wrong with the array syntax?
Your problem is your formatting. Let's break it down:
titleDims = [{
Month: comboBox1.text,
Year:comboBox2.text,
Sales Order:comboBox3.text // Whoops! There's a space here!
}]
I suggest to change it to SalesOrder instead.
If you really need spaces in the key, you can do this:
titleDims = [{
'Month': comboBox1.text,
'Year': comboBox2.text,
'Sales Order': comboBox3.text
}]
cb1 = comboBox1; cb2 = comboBox2; cb3 = comboBox3;
Option A
titleDims="[{'Month': cb1.text, 'Year':cb2.text, 'Sales Order':cb3.text}]";
Option B
titleDims="[{Month: cb1.text, Year:cb2.text, SalesOrder:cb3.text}]";
Option C
titleDims="[{Month: cb1.text, Year:cb2.text, Sales_Order:cb3.text}]";
I'm ignoring your use of setting titleDims to a string first and assuming you have some code that needs it that way. In the future, you don't need to quote this declaration.

Resources