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
-
#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
- #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.)
58 59 60 61 62 |
# File 'lib/mongo/query.rb', line 58 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
#number_to_return ⇒ Object
Returns the value of attribute number_to_return.
29 30 31 |
# File 'lib/mongo/query.rb', line 29 def number_to_return @number_to_return end |
#number_to_skip ⇒ Object
Returns the value of attribute number_to_skip.
29 30 31 |
# File 'lib/mongo/query.rb', line 29 def number_to_skip @number_to_skip end |
#order_by ⇒ Object
Returns the value of attribute order_by.
29 30 31 |
# File 'lib/mongo/query.rb', line 29 def order_by @order_by end |
#selector ⇒ Object
writer defined below
30 31 32 |
# File 'lib/mongo/query.rb', line 30 def selector @selector end |
Instance Method Details
#fields ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/mongo/query.rb', line 84 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.
79 80 81 82 |
# File 'lib/mongo/query.rb', line 79 def fields=(val) @fields = val @fields = nil if @fields && @fields.empty? end |