lib/dynamoid/finders.rb
def find_all_by_secondary_index(hash, options = {})
        range = options[:range] || {}
        hash_key_field, hash_key_value = hash.first
        range_key_field, range_key_value = range.first
        range_op_mapped = nil

        if range_key_field
          range_key_field = range_key_field.to_s
          range_key_op = "eq"
          if range_key_field.include?(".")
            range_key_field, range_key_op = range_key_field.split(".", 2)
          end
          range_op_mapped = RANGE_MAP.fetch(range_key_op)
        end

        # Find the index
        index = self.find_index(hash_key_field, range_key_field)
        raise Dynamoid::Errors::MissingIndex if index.nil?

        # query
        opts = {
          :hash_key => hash_key_field.to_s,
          :hash_value => hash_key_value,
          :index_name => index.name,
        }
        if range_key_field
          opts[:range_key] = range_key_field
          opts[range_op_mapped] = range_key_value
        end
        Dynamoid.adapter.query(self.table_name, opts).map do |item|
          from_database(item)
        end
      end
# Find all objects by using local secondary or global secondary index
#
# @example
#   class User
#     include Dynamoid::Document
#     field :email,          :string
#     field :age,            :integer
#     field :gender,         :string
#     field :rank            :number
#     table :key => :email
#     global_secondary_index :hash_key => :age, :range_key => :rank
#   end
#   # NOTE: the first param and the second param are both hashes,
#   #       so curly braces must be used on first hash param if sending both params
#   User.find_all_by_secondary_index({:age => 5}, :range => {"rank.lte" => 10})
#
# @param [Hash] eg: {:age => 5}
# @param [Hash] eg: {"rank.lte" => 10}
# @param [Hash] options - @TODO support more options in future such as
#               query filter, projected keys etc
# @return [Array] an array of all matching items
Find all objects by using local secondary or global secondary index
Find all objects by using local secondary or global secondary index
def find_all_by_secondary_index(hash, options = {})
        range = options[:range] || {}
        hash_key_field, hash_key_value = hash.first
        range_key_field, range_key_value = range.first
        range_op_mapped = nil
        if range_key_field
          range_key_field = range_key_field.to_s
          range_key_op = "eq"
          if range_key_field.include?(".")
            range_key_field, range_key_op = range_key_field.split(".", 2)
          end
          range_op_mapped = RANGE_MAP.fetch(range_key_op)
        end
        index = self.find_index(hash_key_field, range_key_field)
        raise Dynamoid::Errors::MissingIndex if index.nil?
        opts = {
          :hash_key => hash_key_field.to_s,
          :hash_value => hash_key_value,
          :index_name => index.name,
        }
        if range_key_field
          opts[:range_key] = range_key_field
          opts[range_op_mapped] = range_key_value
        end
        Dynamoid.adapter.query(self.table_name, opts).map do |item|
          from_database(item)
        end
      end
def find_all_by_secondary_index ( hash , options = { } ) range = options [ :range ] || { } hash_key_field , hash_key_value = hash . first range_key_field , range_key_value = range . first range_op_mapped = nil if range_key_field range_key_field = range_key_field . to_s range_key_op = "eq" if range_key_field . include? ( "." ) range_key_field , range_key_op = range_key_field . split ( "." , 2 ) end range_op_mapped = RANGE_MAP . fetch ( range_key_op ) end index = self . find_index ( hash_key_field , range_key_field ) raise Dynamoid :: Errors :: MissingIndex if index . nil? opts = { :hash_key => hash_key_field . to_s , :hash_value => hash_key_value , :index_name => index . name , } if range_key_field opts [ :range_key ] = range_key_field opts [ range_op_mapped ] = range_key_value end Dynamoid . adapter . query ( self . table_name , opts ) . map do | item | from_database ( item ) end end
Find all objects by using local secondary or global secondary index
Find all objects by using local secondary or global secondary index
# Find the index # query
an array of all matching items
an array of all matching items
Array
hash
options
@TODO support more options in future such as query filter, projected keys etc
@TODO support more options in future such as query filter projected keys etc
example
class User include Dynamoid::Document field :email, :string field :age, :integer field :gender, :string field :rank :number table :key => :email global_secondary_index :hash_key => :age, :range_key => :rank end # NOTE: the first param and the second param are both hashes, # so curly braces must be used on first hash param if sending both params User.find_all_by_secondary_index({:age => 5}, :range => {"rank.lte" => 10})
class User include Dynamoid :: Document field : email : string field : age : integer field : gender : string field : rank : number table : key = > : email global_secondary_index : hash_key = > : age : range_key = > : rank end # NOTE : the first param and the second param are both hashes # so curly braces must be used on first hash param if sending both params User . find_all_by_secondary_index ( { : age = > 5 } : range = > { " rank . lte " = > 10 } )