Module: Mongoid::Criterion::Exclusion
- Included in:
- Mongoid::Criteria
- Defined in:
- lib/mongoid/criterion/exclusion.rb
Overview
This module contains criteria behaviour for exclusion of values.
Instance Method Summary collapse
-
#excludes(attributes = {}) ⇒ Criteria
Adds a criterion to the
Criteria
that specifies values that are not allowed to match any document in the database. -
#fields(attributes = nil) ⇒ Criteria
Used when wanting to set the fields options directly using a hash instead of going through only or without.
-
#not_in(attributes) ⇒ Criteria
Adds a criterion to the
Criteria
that specifies values where none should match in order to return results. -
#only(*args) ⇒ Criteria
Adds a criterion to the
Criteria
that specifies the fields that will get returned from the Document. -
#without(*args) ⇒ Criteria
Adds a criterion to the
Criteria
that specifies the fields that will not get returned by the document.
Instance Method Details
#excludes(attributes = {}) ⇒ Criteria
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”.
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 |
#fields(attributes = nil) ⇒ Criteria
Used when wanting to set the fields options directly using a hash instead of going through only or without.
38 39 40 |
# File 'lib/mongoid/criterion/exclusion.rb', line 38 def fields(attributes = nil) clone.tap { |crit| crit.[:fields] = attributes || {} } end |
#not_in(attributes) ⇒ Criteria
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”.
55 56 57 |
# File 'lib/mongoid/criterion/exclusion.rb', line 55 def not_in(attributes) update_selector(attributes, "$nin") end |
#only(*args) ⇒ Criteria
#only and #without cannot be used together.
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.
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/mongoid/criterion/exclusion.rb', line 71 def only(*args) clone.tap do |crit| if args.any? crit.[:fields] = {:_type => 1} crit.field_list = args.flatten crit.field_list.each do |f| crit.[:fields][f] = 1 end end end end |
#without(*args) ⇒ Criteria
#only and #without cannot be used together.
Adds a criterion to the Criteria
that specifies the fields that will not get returned by the document.
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/mongoid/criterion/exclusion.rb', line 96 def without(*args) clone.tap do |crit| if args.any? crit.[:fields] = {} args.flatten.each do |f| crit.[:fields][f] = 0 end end end end |