How do you use custom keys inside of firebase .update method? - node.js

When I try to assign a key to in a multi-path .update() like this:
key1 = 'bar';
key2 = 'baz';
db.ref('path').update({
key1: 'value1',
key2: 'value2'});
It literally writes the key as a string to the DB:
/path/key1/value1
/path/key2/value2
QUESTION: how can I get the expected output with dynamic keys?
/path/bar/value1
/path/baz/value2

To get the expected output, you need to use brackets around your [keys]:
key1 = 'bar';
key2 = 'baz';
db.ref('path').update({
[key1]: 'value1',
[key2]: 'value2'});
This will give you:
/path/bar/value1
/path/baz/value2

Related

DynamoDB adds item instead of updates item when using update_item

I have an html table that is filled from a DynamoDB table. Clicking a row pops up an edit form in a modal. The data inputted is sent to a flask server to update the item - using AWS DynamoDB - that was edited in the modal form. Upon reading the AWS documentation for this, the correct method is to use update_item. However, when doing so the item is added again instead of updating the item. I used the AWS here to script the below. In my DynamoDB table, the primary partition key is KEY1 and the primary sort key is KEY2 in the below reference.
table = dynamodb.Table('table_name') #define DynamoDB table
key1 = account_id #string value of account id
key2 = request.form["KEY2"] #this is a read only field in the form, so the key does not get updated here
form_val1 = request.form["input1"]
form_val2 = request.form["input2"]
form_val3 = request.form["input3"]
form_val4 = request.form["input4"]
form_val5 = request.form["input5"]
form_val6 = request.form["input6"]
form_val7 = request.form["input7"]
form_val8 = request.form["input8"]
form_val9 = request.form["input9"]
#update item in dynamo
table.update_item(
Key={
'KEY1': key1, #partition key
'KEY2': key2 #sort key
},
UpdateExpression='SET dbField1 = :val1, dbField2 = :val2, dbField3 = :val3, dbField4 = :val4, dbField5 = :val5, dbField6 = :val6, dbField7 = :val7, dbField8 = :val8, dbField9 = :val9',
ExpressionAttributeValues={
':val1': form_val1,
':val2': form_val2,
':val3': form_val3,
':val4': form_val4,
':val5': form_val5,
':val6': form_val6,
':val7': form_val7,
':val8': form_val8,
':val9': form_val9
}
)
You can't and I will explain to you for what that not is possible.
When you create a table on dynamo DB with key and a order key you automatically create an index between key and sort key. We know an index is inmutable, that means you can't update the keys. Is for that reason that when you update dynamo create a new element.
It's a problem of the definition of your table because you never need to change the key or the sort key. Recreate your table only with the index and not with the sort index (because if your app can change the sort index that make not sense).
Is this the full query? the update_item docs say that TableName is required, which I don't see in your snippet.
From the updateitem docs:
Edits an existing item's attributes, or adds a new item to the table
if it does not already exist.
Make sure that the primary key (partition key and sort key) are unique in your table. If they are not, updateitem will create a new item in the database.
Are you absolutely certain that the primary key for the item already exists in the database?

How to return a new dictionary obtained by updating a 2 level dictionary?

I need to write a function that returns a dictionary obtained by updating a two-level dictionary(dict2). The resulting dictionary (newdict2) contains newdict2[key1][key2] == value. I'm not sure if I have my for loops and if statement set up correctly or how to update the value that needs to be returned.
I have been trying some nested for loops and if statements to no avail. When I try to plug in the testing examples I get a name error. I'll post below. Even though, I'm still not sure my code will work. I receive a nametype error when running test case saying DD is not defined.
my function:
def update_dict2(dict2, key1, key2, value):
newdict2 = {}
for key1 in dict2:
for key2 in key1:
if key1[0] == key2[0]:
newdict2[key1][key2] == value
x = update_dict2(DD,'aaa','ccc',12)
print(x)
DD = {'aaa' : {'bbb': 'string1','ccc': 'string2', 'ddd' : 'string3' },
'bbb' : {'ccc':'string4','ddd':'string5','eee':'string6','fff':'string7'},
'ccc' : {'aaa':'string8','bbb':'string9'}}
Expected results:
1. update_dict2(DD,'aaa','ccc',12)
return value:
{'aaa': { 'bbb' : 'string1', 'ccc' : 12, 'ddd' : 'string3' },
'bbb': {'ccc':'string4','ddd':'string5','eee':'string6','fff':'string7'},
'ccc': { 'aaa' : 'string8', 'bbb' : 'string9' }}
2. update_dict2(DD,'aaa','ggg','string17')
return value:
{'aaa':{'bbb':'string1','ccc':12,'ddd':'string3','ggg':'string17'},
'bbb':{'ccc':'string4','ddd':'string5','eee':'string6','fff':'string7'},
'ccc':{'aaa':'string8','bbb':'string9'}
}
3. update_dict2(DD,'ggg','aaa','string17'):
enter code here
return value:
{'aaa':{'bbb':'string1','ccc':12,'ddd':'string3'},
'bbb':{'ccc':'string4','ddd':'string5','eee':'string6','fff':'string7'},
'ccc':{'aaa':'string8','bbb':'string9'},
'ggg':{'aaa':'string17'}}
Errors:
Error received when running a test case
[1]: https://i.stack.imgur.com/JHA4t.png
'''This works'''
def update_dict2(dict2, key1, key2, value):
'''
This function takes a two-level dicitonary and updates the values at key2 if
key1 exists. If key1 does not exist, the function adds it to the dictionary with
it's coinciding key2 and value.
'''
if key1 not in dict2:
dict2[key1] = {key2:value}
else:
dict2[key1][key2] = value
return dict2

create array of objects (multiple keys and values)

I created var object then put in key and values.
I can call key and object[key] however there are some keys that are repeated such as
key = A,
key = B,
key = A,
key = B
I want to separate the first two objects in array[0][0] and array[0][1]
and the other two in array[1][0] and array[1][1]. Is this possible?
Thanks in advance!
Deending on your needs, you can make an array of arrays as such.
var arr = new Array();
arr.push([{key:"A"}, {key:"B"}]);
arr.push([{key:"A"}, {key:"B"}]);
console logs =>
arr[0][0]
Object {key: "A"}
arr[0][1]
Object {key: "B"}
arr[1][0]
Object {key: "A"}
arr[1][1]
Object {key: "B"}

Mapping to Dictionary collection in PetaPoco?

way to map the following to a Dictionary??
Sql sql = new Sql()
.Append("SELECT Count(*) ")
.Append("FROM Custumers")
.Append("WHERE CustomerId = #0", Id)
var result = database.Fetch<Dictionary<int,DateTime>>(sql);
I cannot use List as DateTime is also thr.
Petapoco always return a List<T>, but you can convert the List to a Dictionary afterwards:
var result = database
.Fetch<Pair<int,DateTime>>(sql)
.ToDictionary(i => i.ID, i => i.Date);
With NPoco you can write this: it will use the first 2 columns
var result = database.Dictionary<int, DateTime>(sql);
or use what #Eduardo said.

Join by two slightly different keys in Pig

A mapreduce task:
Key1 in file_one is a1,a2,a3,a10,a11,a12;
Key2 in file_two is persona1, persona1, persona2, persona3, persona12,persona12,persona3, persona11, persona10.
Merge_file=JOIN file_one BY Key1, file_two by Key2??(how to write this..)
And since the second key has duplication, does it matter?
thanks
My suggestion would be to create a new column for each dataset and join on that, eg.:
A = foreach file_one generate *, join_key1 as SUBSTRING(key1, 1, 100);
B = foreach file_two generate *, join_key2 as SUBSTRING(key2, 7, 100);
C = join A by join_key1, B by join_key2;

Resources