Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
590 views
in Technique[技术] by (71.8m points)

javascript - How to put the item in DynamoDB with conditional expression that is not Primary Key

After generating a random uuid value I go ahead and assign it to the item.uuid:

let uuid = uuidv4();
let title = "Title 100"
let item = {
  uuid: uuid,
  title: title  
};

With the Dynamodb table where the Primary Key is the item.uuid attribute, I add the item to the table with:

let TABLE_NAME = "My Table Name";
let options = {};

let promise = dbClient.put_item({ Item: item, TableName: TABLE_NAME, ...options });

The new item is inserted into the Dynamodb table with the Primary Key uuid generated earlier.

Next, I want to add another item to the same table, with the same "title 101" title:

let uuid = uuidv4();
let title = "title 101"

let item = {
  uuid: uuid,
  title: title  
};

( both items will have the same title, but different uuid used as a Primary Key in a table).

Since I want to make sure, this item will be added only if no other items have the same "title 101" title, I use the ConditionExpression:

let options = {
    ConditionExpression: "#title <> :title",
    ExpressionAttributeNames: { 
        "#title": "title" 
     },
    ExpressionAttributeValues: {
        ":title" : "title 101"
    }
}

let promise = dbClient.put_item({ Item: item, TableName: TABLE_NAME, ...options });
promise
.then(res => {
  console.log('--------- SUCCESS --------');
  console.log(res);
  console.log('-----------------');
});

But regardless of the ConditionExpression used here, the DynamoDB goes ahead and adds a new item with the same title value.

Is there a way to use the conditional expression on an attribute (field) that is not a Primary Key?

question from:https://stackoverflow.com/questions/65894824/how-to-put-the-item-in-dynamodb-with-conditional-expression-that-is-not-primary

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Condition Expressions are only applicable for update/delete(not for create), i.e if an item already exists for the given primary key(PartitionKey + HashKey), then checks the result of condition expression and proceeds if it returns true. We still need to pass entire Primary Key.

We can try something with scan + filter, but we will end up reading entire table to find a row matching a non keyed attribute, which is in efficient and will cost a fortune if table is big.

so, best thing in this case is adding a Global Secondary Index on title and run two separate queries, First query on GSI by title to determine if record exists with matching title and put-item if doesn't exist.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...