Method: Mongoid::Criterion::Inclusion#or

Defined in:
lib/mongoid/criterion/inclusion.rb

#orCriteria

Adds a criterion to the Criteria that specifies a set of expressions to match if any of them return true. This is a $or query in MongoDB and is similar to a SQL OR. This is named #any_of and aliased “or” for readability.

Examples:

Adding the criterion.

criteria.any_of({ :field1 => "value" }, { :field2 => "value2" })

Parameters:

  • args (Array<Hash>)

    A list of name/value pairs any can match.

Returns:

  • (Criteria)

    A new criteria with the added selector.



97
98
99
100
101
102
103
104
# File 'lib/mongoid/criterion/inclusion.rb', line 97

def any_of(*args)
  clone.tap do |crit|
    criterion = @selector["$or"] || []
    converted = BSON::ObjectId.convert(klass, args.flatten)
    expanded = converted.collect { |hash| hash.expand_complex_criteria }
    crit.selector["$or"] = criterion.concat(expanded)
  end
end