Class: XGen::Mongo::Driver::Query
- Inherits:
-
Object
- Object
- XGen::Mongo::Driver::Query
- Defined in:
- lib/mongo/query.rb
Overview
A query against a collection. A query’s selector is a hash. See the Mongo documentation for query details.
Instance Attribute Summary collapse
-
#explain ⇒ Object
If true, $explain will be set in QueryMessage that uses this query.
-
#hint_fields ⇒ Object
Either
nilor an array of hint field names. -
#number_to_return ⇒ Object
Returns the value of attribute number_to_return.
-
#number_to_skip ⇒ Object
Returns the value of attribute number_to_skip.
-
#order_by ⇒ Object
Returns the value of attribute order_by.
-
#selector ⇒ Object
writer defined below.
Instance Method Summary collapse
- #contains_special_fields ⇒ Object
- #fields ⇒ Object
-
#fields=(val) ⇒ Object
Set fields to return.
-
#initialize(sel = {}, return_fields = nil, number_to_skip = 0, number_to_return = 0, order_by = nil) ⇒ Query
constructor
- sel
-
A hash describing the query.
Constructor Details
#initialize(sel = {}, return_fields = nil, number_to_skip = 0, number_to_return = 0, order_by = nil) ⇒ Query
- sel
-
A hash describing the query. See the Mongo docs for details.
- return_fields
-
If not
nil, a single field name or an array of field names. Only those fields will be returned. (Called :fields in calls to Collection#find.) - number_to_skip
-
Number of records to skip before returning records. (Called :offset in calls to Collection#find.) Default is 0.
- number_to_return
-
Max number of records to return. (Called :limit in calls to Collection#find.) Default is 0 (all records).
- order_by
-
If not
nil, specifies record sort order. May be a String, Hash, OrderedHash, or Array. If a string, the results will be ordered by that field in ascending order. If an array, it should be an array of field names which will all be sorted in ascending order. If a hash, it may be either a regular Hash or an OrderedHash. The keys should be field names, and the values should be 1 (ascending) or -1 (descending). Note that if it is a regular Hash then sorting by more than one field probably will not be what you intend because key order is not preserved. (order_by is called :sort in calls to Collection#find.)
61 62 63 64 65 |
# File 'lib/mongo/query.rb', line 61 def initialize(sel={}, return_fields=nil, number_to_skip=0, number_to_return=0, order_by=nil) @number_to_skip, @number_to_return, @order_by = number_to_skip, number_to_return, order_by self.selector = sel self.fields = return_fields end |
Instance Attribute Details
#explain ⇒ Object
If true, $explain will be set in QueryMessage that uses this query.
30 31 32 |
# File 'lib/mongo/query.rb', line 30 def explain @explain end |
#hint_fields ⇒ Object
Either nil or an array of hint field names.
32 33 34 |
# File 'lib/mongo/query.rb', line 32 def hint_fields @hint_fields end |
#number_to_return ⇒ Object
Returns the value of attribute number_to_return.
28 29 30 |
# File 'lib/mongo/query.rb', line 28 def number_to_return @number_to_return end |
#number_to_skip ⇒ Object
Returns the value of attribute number_to_skip.
28 29 30 |
# File 'lib/mongo/query.rb', line 28 def number_to_skip @number_to_skip end |
#order_by ⇒ Object
Returns the value of attribute order_by.
28 29 30 |
# File 'lib/mongo/query.rb', line 28 def order_by @order_by end |
#selector ⇒ Object
writer defined below
33 34 35 |
# File 'lib/mongo/query.rb', line 33 def selector @selector end |
Instance Method Details
#contains_special_fields ⇒ Object
104 105 106 |
# File 'lib/mongo/query.rb', line 104 def contains_special_fields (@order_by != nil && @order_by.length > 0) || @explain || @hint_fields end |
#fields ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/mongo/query.rb', line 87 def fields case @fields when String {@fields => 1} when Array if @fields.length == 0 nil else h = {} @fields.each { |field| h[field] = 1 } h end else # nil, anything else nil end end |
#fields=(val) ⇒ Object
Set fields to return. If val is nil or empty, all fields will be returned.
82 83 84 85 |
# File 'lib/mongo/query.rb', line 82 def fields=(val) @fields = val @fields = nil if @fields && @fields.empty? end |