xaisoft
xaisoft

Reputation: 3451

Check if an item exists with DynamoDbContext?

Is there a way to check if an item exists in DynamoDbContext that does not return the item? Currently, in .NET I am using Load, but this returns the item. Is there a method that just returns true or false based on existence?

Upvotes: 0

Views: 3208

Answers (2)

mujtaba
mujtaba

Reputation: 39

You can build an expression as below and use in your query. And then you can check the count.

var expression = new Expression
{
  ExpressionStatement = "attribute_exists(attributeName)"
};

Upvotes: 0

robnick
robnick

Reputation: 1768

March 2018: I've done a search of the current assembly AWSSDK.DynamoDBv2, Version=3.3.0.0 and can't find any method that resembles an EXISTS style method.

Per the comment above from Anthony Neace, and the answer by Sony Kadavan:

The bottleneck is in latency to reach the Dynamo DB servers (REST API) and in fetching from the index. So Getting and checking will be similar speed. Quickly query a table if it contains a key (DynamoDB and Java)

So I believe the answer is no there's no such method. You can write your own wrapper that gets the item (maybe get just one attribute) and return a boolean indicating if the item exists or not.

This is from the AWS doco and shows how to query a table returning a defined set of attributes:

Table table = Table.LoadTable(client, "Reply");
DateTime twoWeeksAgoDate = DateTime.UtcNow - TimeSpan.FromDays(15);
QueryOperationConfig config = new QueryOperationConfig()
{
  HashKey = "DynamoDB Thread 2", //Partition key
  AttributesToGet = new List<string> 
    { "Subject", "ReplyDateTime", "PostedBy" },
  ConsistentRead = true,
  Filter = new RangeFilter(QueryOperator.GreaterThan, twoWeeksAgoDate)
};

Search search = table.Query(config);

In your case just get a single attribute and returns a true/false depending on whether you got the item or not.

Upvotes: 2

Related Questions