lib/mongo/collection/view/readable.rb
# Get a list of distinct values for a specific field.
#
# @example Get the distinct values.
#   collection_view.distinct('name')
#
# @param [ String, Symbol ] field_name The name of the field.
# @param [ Hash ] opts Options for the distinct command.
#
# @option opts :max_time_ms [ Integer ] The maximum amount of time to allow the
#   command to run.
# @option opts [ Hash ] :read The read preference options.
# @option opts [ Hash ] :collation The collation to use.
#
# @return [ Array<Object> ] The list of distinct values.
#
# @since 2.0.0
Get a list of distinct values for a specific field.
Get a list of distinct values for a specific field .
def distinct(field_name, opts = {})
          if field_name.nil?
            raise ArgumentError, 'Field name for distinct operation must be not nil'
          end
          cmd = { :distinct => collection.name,
                  :key => field_name.to_s,
                  :query => filter }
          cmd[:maxTimeMS] = opts[:max_time_ms] if opts[:max_time_ms]
          if read_concern
            cmd[:readConcern] = Options::Mapper.transform_values_to_strings(
              read_concern)
          end
          Mongo::Lint.validate_underscore_read_preference(opts[:read])
          read_pref = opts[:read] || read_preference
          selector = ServerSelector.get(read_pref || server_selector)
          with_session(opts) do |session|
            read_with_retry(session, selector) do |server|
              Operation::Distinct.new(
                selector: cmd,
                db_name: database.name,
                options: {:limit => -1},
                read: read_pref,
                session: session,
                collation: opts[:collation] || opts['collation'] || collation,
              ).execute(server, context: Operation::Context.new(client: client, session: session))
            end.first['values']
          end
        end
def distinct ( field_name , opts = { } ) if field_name . nil? raise ArgumentError , 'Field name for distinct operation must be not nil' end cmd = { :distinct => collection . name , :key => field_name . to_s , :query => filter } cmd [ :maxTimeMS ] = opts [ :max_time_ms ] if opts [ :max_time_ms ] if read_concern cmd [ :readConcern ] = Options :: Mapper . transform_values_to_strings ( read_concern ) end Mongo :: Lint . validate_underscore_read_preference ( opts [ :read ] ) read_pref = opts [ :read ] || read_preference selector = ServerSelector . get ( read_pref || server_selector ) with_session ( opts ) do | session | read_with_retry ( session , selector ) do | server | Operation :: Distinct . new ( selector : cmd , db_name : database . name , options : { :limit => - 1 } , read : read_pref , session : session , collation : opts [ :collation ] || opts [ 'collation' ] || collation , ) . execute ( server , context : Operation :: Context . new ( client : client , session : session ) ) end . first [ 'values' ] end end
Get a list of distinct values for a specific field.
Get a list of distinct values for a specific field .
# For some reason collation was historically accepted as a # string key. Note that this isn't documented as valid usage.
The list of distinct values.
The list of distinct values .
 Array<Object> 
field_name
<not_specific>
opts
:collation The collation to use.
: collation The collation to use .
<not_specific>
opts Options for the distinct command.
opts Options for the distinct command .
example
Get the distinct values. collection_view.distinct('name')
Get the distinct values . collection_view . distinct ( ' name ' )
since
None