I have a list of elements in database that looks like this:
User
---inventory(Map)
------items(List)
---------item0(Map)
---------item1(Map)
---------item2(Map)
I would like to delete one of the items based on item characteristics (e.g. item.id)
How can I do that with conditional expression? From what I've read you can only remove list elements using list indexes:
REMOVE MyNumbers[1], MyNumbers[3]
How can I do this with UpdateExpression/ConditionExpression instead?
I believe the ConditionExpression is only for deciding whether to run/skip your whole update command.
In your case, you probably need to do a getItem to search for the index of remove target and remove the list item by index on updateItem.
You can also consider changing the List to Map and use the item's ID as key.
{
inventory: {
items :{
1 : { name : item1...},
2 : { name : item1...},
}
}
}
This way you can just use one db update command to remove the record.
remove inventory.items.1
Related
In DynamoDb, is it possible to do a conditional put and return the old item if there already was a matching item?
I would like something like the following to create the row if the user does not already exist, and if it does exist, I want it to return the old item (with whatever name was there before). I.e., insert item if new, else just read existing item.
await documentClient.put({
TableName: 'table',
Item: {
userId: 'user0',
name: 'smith',
},
ConditionExpression: 'attribute_not_exists(userId)',
});
Is this possible to do in one call?
Sort of.
If you use UpdateItem it will create or update the item. It only updates the fields that have changed however, so if your fields have not changed, then no update will be made. It will also create the item if you do not have it.
Using Conditional Items for this will return an ErrorCode if the condition fails ConditionalCheckFailedException - which would require an additional operation.
(I don't use the javascript SDK that much, more in python, so I'm not sure this is the correct version/page but here is some documentation:
https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#updateItem-property )
My top level state model looks like this:
{
listOfItems: [], // Item[]
selections: {
itemId: 0
}
}
The items list may contain 10 different shopping items.
When the user clicks on an item, it updates the selection, and my #Selector will rerun.
Action: Set Item Selection
#Action(Item.Select)
setState(
{ setState }: StateContext<ItemsModel>,
{ itemId }: Item.Select
) {
setState(patch({ selections: patch({ itemId }) }));
}
Selector: Select Current Item
#Selector()
static getSelectedItem(state: ItemModel): Item {
return state.itemList.find(i => i.itemId === state.selections.itemId);
}
Problem is: I have up to 20 actions to perform on the selected Item. This results in:
Lots of .find() lookups to find item in the original array (both selector and actions)
Actions to perform on the listOfItems are in the same place as those to perform on a specific Item
I would like to: Keep the array and selection in this state, but separate out the "selected item" into a new substate, where the child state's model can just be Item type. This way I can encapsulate all the actions on Item in a different place to actions on the Items[] array.
I'm not sure how to keep them in sync. I need to keep the 'selectedItem' state up to date when the selection itemId changes in the parent. I also need to make sure any mutations to the selectedItem are reflected in the original array in the parent.
This seems like it might be more of a fundamental problem with how you are trying to represent your application state. Have you thought of normalizing your list of items? Or at least using a key/value lookup object instead of an array? You wouldn't need to use the .find() to do your lookup and could access the key of the object via the unique id you are interested in. Let me know if that is of any help!
I want to return a $firebaseArray of records that contain a particular value without knowing the parent ID.
So in this case, I want to get all the objects under 'challenges' that contain the ID pictured above starting with LNF.
I have tried using orderByChild and equalTo, but again this seems to requiring knowing the parent ID. Is there a way around this?
If you want everything with key CPjip3tVE1Q067Qzx6p0vij0k1|1try this:
var key = "CPjip3tVE1Q067Qzx6p0vij0k1|1";
for child in snapshot.children {
if child.key == key {
get your data
}
}
You can use that to loop over all of the children under "challenges"
How to replace a filed value in solrcloud. e.g update a collection field value?
The set modifier is basically used to do replace.Below is the snippet you can use to update the collection
Below is the following document exist in our collection
{
"id":"mydoc",
"price":10,
"popularity":42,
}
And when we apply the update command
{
"id":"mydoc",
"price":{"set":99},
"popularity":{"inc":20}
}
The resulting document will be as below.
{
"id":"mydoc",
"price":99,
"popularity":62,
}
I am having an issue in Extjs5 when retrieving a specific group data from my store using getGroups() function.
In Extjs 4 it works fine :
Ext.getCmp('Grid').getStore().getGroups(groupName).children;
But when I am using Extjs5 I can't assign a parameter to the getGroups() and I can't get its children.
How can I get the children of a specific store group in Extjs5 ?
Thanks in advance
I think, you need to change the getGroups to make it work.
Ext.define('Ext.csx.data.BufferedJsonStore', {
extend: 'Ext.data.Store',
getGroups: function (requestGroupString) {
if (!this.data.items) {
return [];
}
this.callParent(arguments);
}
});
According to the documentation for Store.getGroups, it returns an array of the grouped data. So you can find a specific group, and its children, by looping through the array to find an entry with the name property set to the group name, then getting the children property of that.
Or you could simply apply a filter to the store, based on the group field.