Class: Mongo::Query
- Inherits:
-
Object
- Object
- Mongo::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 ⇒ Object
Either
nil
or a hash (preferably an OrderedHash). -
#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.
-
#snapshot ⇒ Object
Returns the value of attribute snapshot.
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, hint = nil, snapshot = 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, hint = nil, snapshot = 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.) - hint
-
If not
nil
, specifies query hint fields. Must be either+nil+ or a hash (preferably an OrderedHash). See Collection#hint.
64 65 66 67 68 69 70 |
# File 'lib/mongo/query.rb', line 64 def initialize(sel={}, return_fields=nil, number_to_skip=0, number_to_return=0, order_by=nil, hint=nil, snapshot=nil) @number_to_skip, @number_to_return, @order_by, @hint, @snapshot = number_to_skip, number_to_return, order_by, hint, snapshot @explain = nil 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.
29 30 31 |
# File 'lib/mongo/query.rb', line 29 def explain @explain end |
#hint ⇒ Object
Either nil
or a hash (preferably an OrderedHash).
31 32 33 |
# File 'lib/mongo/query.rb', line 31 def hint @hint end |
#number_to_return ⇒ Object
Returns the value of attribute number_to_return.
27 28 29 |
# File 'lib/mongo/query.rb', line 27 def number_to_return @number_to_return end |
#number_to_skip ⇒ Object
Returns the value of attribute number_to_skip.
27 28 29 |
# File 'lib/mongo/query.rb', line 27 def number_to_skip @number_to_skip end |
#order_by ⇒ Object
Returns the value of attribute order_by.
27 28 29 |
# File 'lib/mongo/query.rb', line 27 def order_by @order_by end |
#selector ⇒ Object
writer defined below
32 33 34 |
# File 'lib/mongo/query.rb', line 32 def selector @selector end |
#snapshot ⇒ Object
Returns the value of attribute snapshot.
27 28 29 |
# File 'lib/mongo/query.rb', line 27 def snapshot @snapshot end |
Instance Method Details
#contains_special_fields ⇒ Object
111 112 113 |
# File 'lib/mongo/query.rb', line 111 def contains_special_fields (@order_by != nil && @order_by.length > 0) || @explain || @hint || @snapshot end |
#fields ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/mongo/query.rb', line 94 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.
89 90 91 92 |
# File 'lib/mongo/query.rb', line 89 def fields=(val) @fields = val @fields = nil if @fields && @fields.empty? end |