Class: XGen::Mongo::Driver::Query

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(sel = {}, return_fields = nil, number_to_skip = 0, number_to_return = 0, order_by = nil, hint = 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.


65
66
67
68
69
70
# File 'lib/mongo/query.rb', line 65

def initialize(sel={}, return_fields=nil, number_to_skip=0, number_to_return=0, order_by=nil, hint=nil)
  @number_to_skip, @number_to_return, @order_by, @hint =
    number_to_skip, number_to_return, order_by, hint
  self.selector = sel
  self.fields = return_fields
end

Instance Attribute Details

#explainObject

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

#hintObject

Either nil or a hash (preferably an OrderedHash).



32
33
34
# File 'lib/mongo/query.rb', line 32

def hint
  @hint
end

#number_to_returnObject

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_skipObject

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_byObject

Returns the value of attribute order_by.



28
29
30
# File 'lib/mongo/query.rb', line 28

def order_by
  @order_by
end

#selectorObject

writer defined below



33
34
35
# File 'lib/mongo/query.rb', line 33

def selector
  @selector
end

Instance Method Details

#contains_special_fieldsObject



109
110
111
# File 'lib/mongo/query.rb', line 109

def contains_special_fields
  (@order_by != nil && @order_by.length > 0) || @explain || @hint
end

#fieldsObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/mongo/query.rb', line 92

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.



87
88
89
90
# File 'lib/mongo/query.rb', line 87

def fields=(val)
  @fields = val
  @fields = nil if @fields && @fields.empty?
end