Skip to content

Instantly share code, notes, and snippets.

@8parth
Last active March 8, 2017 18:23
Show Gist options
  • Save 8parth/8edf9def1a77d40d80def035b3899fff to your computer and use it in GitHub Desktop.
Save 8parth/8edf9def1a77d40d80def035b3899fff to your computer and use it in GitHub Desktop.
dynamodb query examples
ddb = Aws::DynamoDB::Client.new({
access_key_id: '',
secret_access_key: '',
region: ''
})
# or, if you want to connect to local dynamodb database,
ddb = Aws::DynamoDB::Client.new(endpoint: 'http://localhost:8000')
# suppose table is created with following partition_key and range_key,
# partition_key => "partition_key"
# range_key => "created_at"
# global index (to allow querying through some other field than primary partition key) :
# index_key => "some_index_key"
# range_key => "created_at"
# recrods matching given partition_key
ddb.query({
expression_attribute_values: {
":partition_key" => partition_key_value,
},
key_condition_expression: "partition_key = :partition_key",
table_name: "TableName"
});
# records matching given partition key and between given range of created at field
$ddb.query({
expression_attribute_values: {
":bot_id" => bot_id,
":start_date" => start_date,
":end_date" => end_date
},
key_condition_expression: "partition_key = :partition_key AND created_at BETWEEN :start_date AND :end_date",
table_name: "TableName"
});
# records matching given global index
ddb.query({
table_name: "TableName",
index_name: "some_index_key-created_at-index",
expression_attribute_values: {
":some_index_key" => index_key_value
},
key_condition_expression: "some_index_key = :some_index_key"
});
# records matching given gloabl index key and between given range of created at field
ddb.query({
table_name: "TableName",
index_name: "some_index_key-created_at-index",
expression_attribute_values: {
":some_index_key" => index_key_value,
":start_date" => start_time,
":end_date" => end_time
},
key_condition_expression: "some_index_key = :some_index_key AND created_at BETWEEN :start_date AND :end_date"
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment