vatti
vatti

Reputation: 473

How to query multiple items in DynamoDB (iOS)?

I would like to get the items of many videoIds, not like in this code only one video Item? Is there a way to query multiple items from an index?

AWSDynamoDBObjectMapper *objectMapper = [AWSDynamoDBObjectMapper defaultDynamoDBObjectMapper];
AWSDynamoDBQueryExpression *queryExpression = [AWSDynamoDBQueryExpression new];

queryExpression.indexName = @"getVideoItem";
queryExpression.keyConditionExpression = @"#id = :id";
queryExpression.expressionAttributeNames = @{
                                             @"#id" : @"id",
                                             };
queryExpression.expressionAttributeValues = @{
                                              @":id" : videoId,
                                              };
__weak typeof(self) weakSelf = self;

[objectMapper query:[PublicVideos class]
         expression:queryExpression
  completionHandler:^(AWSDynamoDBPaginatedOutput * _Nullable response, NSError * _Nullable error) {
      if (!error) {
          dispatch_async(dispatch_get_main_queue(), ^{
              if ([weakSelf.delegate respondsToSelector:@selector(ServerConnectionHandlerVideoListRecevied:)]) {
                  [weakSelf.delegate ServerConnectionHandlerVideoListRecevied:response.items];
              }
          });
      }
  }];

Upvotes: 0

Views: 469

Answers (1)

Rohan Dubal
Rohan Dubal

Reputation: 847

What does the schema of your table look like? Do you have a hash key and range key both enabled for your table? If you don't have any range key, you will only have 1 entry per hash key and your querying will return only one row.

If you have both hash key and range key, you query by something like this:

AWSDynamoDBObjectMapper *objectMapper = [AWSDynamoDBObjectMapper defaultDynamoDBObjectMapper];
AWSDynamoDBQueryExpression *queryExpression = [AWSDynamoDBQueryExpression new];
queryExpression.indexName = @"Artist-Album-index";
queryExpression.keyConditionExpression = @"#artist = :artist AND #album < :album";
queryExpression.expressionAttributeNames = @{
                                                 @"#artist" : @"Artist",
                                                 @"#album" : @"Album",
                                                 };
queryExpression.expressionAttributeValues = @{
                                                  @":artist" : @"valuetosearch",
                                                  @":album" : @"valuetosearch",
                                                  };
[objectMapper query:[Song class]
             expression:queryExpression
      completionHandler:^(AWSDynamoDBPaginatedOutput * _Nullable response, NSError * _Nullable error) {
          dispatch_async(dispatch_get_main_queue(), ^{
              // handle response
          });
      }];

Here in the above snippet, it tries looking for all albums for an artist matching a condition of '<'

Upvotes: 1

Related Questions