Deleting multiple records in dynamo db

后端 未结 4 915
轮回少年
轮回少年 2021-01-12 22:54

can we delete multiple records in dynamo db only through range key?

I\'m trying to do like this sql statement

delete from employee where emplo

相关标签:
4条回答
  • 2021-01-12 23:12

    Based on the API, there is no Batch Delete for DynamoDB.

    http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html

    You need to query the item and delete with their partition key.

    I would recommend looking at ODM,

    https://github.com/clarkie/dynogels

    to make the operations simpler.

    Check out the sample calls for deletion,

    Account.destroy('foo@example.com', function (err) {
      console.log('account deleted');
    });
    
    // Destroy model using hash and range key
    BlogPost.destroy('foo@example.com', 'Hello World!', function (err) {
      console.log('post deleted')
    });
    
    BlogPost.destroy({email: 'foo@example.com', title: 'Another Post'}, function (err) {
      console.log('another post deleted')
    });
    
    0 讨论(0)
  • 2021-01-12 23:16

    You would have to do a BatchWriteItem to delete your items. From the documentation:

    BatchWriteItem performs put and delete

    0 讨论(0)
  • 2021-01-12 23:19

    @Kannaiyan thanks for your comments....

    i found the solution with dynamoDB with java http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/JavaDocumentAPICRUDExample.html

    DeleteItemSpec deleteItemSpec = new DeleteItemSpec().withPrimaryKey("Id", 120)
                    .withConditionExpression("#ip = :val").withNameMap(new NameMap().with("#ip", "InPublication"))
                    .**withValueMap(new ValueMap()**.withBoolean(":val", false)).withReturnValues(ReturnValue.ALL_OLD);
    

    fetching the records with key, value pair and storing it in "withValueMap(new ValueMap()" and this helps me.

    Thanks.

    0 讨论(0)
  • 2021-01-12 23:23

    Please Check This query it's working with multiple data

    const params = {TableName: "test"};
    
    docClient.scan(params, (error, result) => {
            if (error) { console.log(error,"error scan"); }
    
            result.Items.forEach(function(item) {          
                docClient.delete({Key:{mainID: item.id},TableName:"test"}, (error) => {
                    if (error) {
                        console.log("Delete fail");
                    }
                    console.log("Delete  Success");
                });
            });
    
    });    
    
    0 讨论(0)
提交回复
热议问题