Module: Mongoid::Criterion::Inclusion

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

Instance Method Summary collapse

Instance Method Details

#all(attributes = {}) ⇒ Object Also known as: all_in

Adds a criterion to the Criteria that specifies values that must all be matched in order to return results. Similar to an “in” clause but the underlying conditional logic is an “AND” and not an “OR”. The MongoDB conditional operator that will be used is “$all”.

Options:

attributes: A Hash where the key is the field name and the value is an Array of values that must all match.

Example:

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

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

Returns: self



22
23
24
# File 'lib/mongoid/criterion/inclusion.rb', line 22

def all(attributes = {})
  update_selector(attributes, "$all")
end

#and(selector = nil) ⇒ Object

Adds a criterion to the Criteria that specifies values that must be matched in order to return results. This is similar to a SQL “WHERE” clause. This is the actual selector that will be provided to MongoDB, similar to the Javascript object that is used when performing a find() in the MongoDB console.

Options:

selectior: A Hash that must match the attributes of the Document.

Example:

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

Returns: self



42
43
44
# File 'lib/mongoid/criterion/inclusion.rb', line 42

def and(selector = nil)
  where(selector)
end

#in(attributes = {}) ⇒ Object Also known as: any_in

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

Options:

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

Example:

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

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

Returns: self



62
63
64
# File 'lib/mongoid/criterion/inclusion.rb', line 62

def in(attributes = {})
  update_selector(attributes, "$in")
end

#near(attributes = {}) ⇒ Object

Adds a criterion to the Criteria that specifies values to do geospacial searches by. The field must be indexed with the “2d” option.

Options:

attributes: A Hash where the keys are the field names and the values are Arrays of [latitude, longitude] pairs.

Example:

criteria.near(:field1 => [30, -44])

Returns: self



80
81
82
# File 'lib/mongoid/criterion/inclusion.rb', line 80

def near(attributes = {})
  update_selector(attributes, "$near")
end

#where(selector = nil) ⇒ Object

Adds a criterion to the Criteria that specifies values that must be matched in order to return results. This is similar to a SQL “WHERE” clause. This is the actual selector that will be provided to MongoDB, similar to the Javascript object that is used when performing a find() in the MongoDB console.

Options:

selectior: A Hash that must match the attributes of the Document.

Example:

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

Returns: self



99
100
101
102
103
104
105
106
107
# File 'lib/mongoid/criterion/inclusion.rb', line 99

def where(selector = nil)
  case selector
  when String
    @selector.update("$where" => selector)
  else
    @selector.update(selector ? selector.expand_complex_criteria : {})
  end
  self
end