Method: AWS::DynamoDB::ItemCollection#query
- Defined in:
- lib/aws/dynamo_db/item_collection.rb
#query(options = {}, &block) ⇒ Object
Note:
This method is only valid for tables with a composite primary key.
Queries the items in the table by primary key values. This operation is generally more efficient than the scan operation, which always scans the whole table. A Query operation searches for a specific range of keys satisfying a given set of key conditions and does not have the added step of filtering out results.
# find all items with a given hash key value
items.query(:hash_value => "abc123")
# get only the colors attribute of each item
items.query(
:hash_value => "abc123",
:select => [:colors])
# find only the items where the range key is between two values
items.query(
:hash_value => "abc123",
:range_value => 1..100
)
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 |
# File 'lib/aws/dynamo_db/item_collection.rb', line 697 def query( = {}, &block) = .merge(:query => true) raise ArgumentError, "a hash key value is required" unless [:hash_value] [:hash_key_value] = format_attribute_value(.delete(:hash_value)) range = .delete(:range_value) range_op = nil value_list = [] if range and range.kind_of?(Range) value_list = [format_attribute_value(range.begin), format_attribute_value(range.end)] range_op = "BETWEEN" elsif range value_list = [format_attribute_value(range)] range_op = "EQ" end RANGE_KEY_OPTIONS.each do |name, op| if value = .delete(name) raise(ArgumentError, "only one range key condition is supported") if range_op range_op = op value_list = [format_attribute_value(value)] end end [:range_key_condition] = { :attribute_value_list => value_list, :comparison_operator => range_op } if range_op if select = .delete(:select) [:item_data] = true [:attributes_to_get] = select.map do |att| att.to_s end unless select == :all end if block each(, &block) else enumerator() end end |