Module: Mongoid::Criterion::Exclusion

Included in:
Mongoid::Criteria
Defined in:
lib/mongoid/criterion/exclusion.rb

Instance Method Summary collapse

Instance Method Details

#excludes(attributes = {}) ⇒ Object

Adds a criterion to the Criteria that specifies values that are not allowed to match any document in the database. The MongoDB conditional operator that will be used is “$ne”.

Options:

attributes: A Hash where the key is the field name and the value is a value that must not be equal to the corresponding field value in the database.

Example:

criteria.excludes(:field => "value1")

criteria.excludes(:field1 => "value1", :field2 => "value1")

Returns: self



21
22
23
24
25
# File 'lib/mongoid/criterion/exclusion.rb', line 21

def excludes(attributes = {})
  mongo_id = attributes.delete(:id)
  attributes = attributes.merge(:_id => mongo_id) if mongo_id
  update_selector(attributes, "$ne")
end

#not_in(exclusions) ⇒ Object

Adds a criterion to the Criteria that specifies values where none should match in order to return results. This is similar to an SQL “NOT IN” clause. The MongoDB conditional operator that will be used is “$nin”.

Options:

exclusions: A Hash where the key is the field name and the value is an Array of values that none can match.

Example:

criteria.not_in(:field => ["value1", "value2"])

criteria.not_in(:field1 => ["value1", "value2"], :field2 => ["value1"])

Returns: self



43
44
45
# File 'lib/mongoid/criterion/exclusion.rb', line 43

def not_in(exclusions)
  exclusions.each { |key, value| @selector[key] = { "$nin" => value } }; self
end

#only(*args) ⇒ Object

Adds a criterion to the Criteria that specifies the fields that will get returned from the Document. Used mainly for list views that do not require all fields to be present. This is similar to SQL “SELECT” values.

Options:

args: A list of field names to retrict the returned fields to.

Example:

criteria.only(:field1, :field2, :field3)

Returns: self



60
61
62
# File 'lib/mongoid/criterion/exclusion.rb', line 60

def only(*args)
  @options[:fields] = args.flatten if args.any?; self
end