Class: Mongo::Cursor
- Includes:
- Enumerable, Conversions
- Defined in:
- lib/mongo/cursor.rb
Overview
A cursor over query results. Returned objects are hashes.
Constant Summary
Constants included from Conversions
Mongo::Conversions::ASCENDING_CONVERSION, Mongo::Conversions::DESCENDING_CONVERSION
Instance Attribute Summary collapse
-
#admin ⇒ Object
readonly
Returns the value of attribute admin.
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#full_collection_name ⇒ Object
readonly
Returns the value of attribute full_collection_name.
-
#hint ⇒ Object
readonly
Returns the value of attribute hint.
-
#order ⇒ Object
readonly
Returns the value of attribute order.
-
#selector ⇒ Object
readonly
Returns the value of attribute selector.
-
#snapshot ⇒ Object
readonly
Returns the value of attribute snapshot.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Instance Method Summary collapse
-
#close ⇒ True
Close the cursor.
-
#closed? ⇒ Boolean
Is this cursor closed?.
-
#count ⇒ Integer
Get the size of the result set for this query.
-
#each { ... } ⇒ Object
Iterate over each document in this cursor, yielding it to the given block.
-
#explain ⇒ Hash
Get the explain plan for this cursor.
-
#initialize(collection, options = {}) ⇒ Cursor
constructor
Create a new cursor.
-
#limit(number_to_return = nil) ⇒ Integer
Limit the number of results to be returned by this cursor.
-
#next_document ⇒ Hash, Nil
Get the next document specified the cursor options.
-
#query_options_hash ⇒ Hash
Get the query options for this Cursor.
-
#query_opts ⇒ Integer
Returns an integer indicating which query options have been selected.
-
#skip(number_to_skip = nil) ⇒ Integer
Skips the first
number_to_skip
results of this cursor. -
#sort(key_or_list, direction = nil) ⇒ Object
Sort this cursor’s results.
-
#to_a ⇒ Array
Receive all the documents from this cursor as an array of hashes.
Methods included from Conversions
#array_as_sort_parameters, #sort_value, #string_as_sort_parameters
Constructor Details
#initialize(collection, options = {}) ⇒ Cursor
Create a new cursor.
Note: cursors are created when executing queries using [Collection#find] and other similar methods. Application developers shouldn’t have to create cursors manually.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/mongo/cursor.rb', line 34 def initialize(collection, ={}) @db = collection.db @collection = collection @connection = @db.connection @selector = convert_selector_for_query([:selector]) @fields = convert_fields_for_query([:fields]) @admin = [:admin] || false @skip = [:skip] || 0 @limit = [:limit] || 0 @order = [:order] @hint = [:hint] @snapshot = [:snapshot] @timeout = [:timeout] || false @explain = [:explain] @socket = [:socket] @full_collection_name = "#{@collection.db.name}.#{@collection.name}" @cache = [] @closed = false @query_run = false end |
Instance Attribute Details
#admin ⇒ Object (readonly)
Returns the value of attribute admin.
22 23 24 |
# File 'lib/mongo/cursor.rb', line 22 def admin @admin end |
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
22 23 24 |
# File 'lib/mongo/cursor.rb', line 22 def collection @collection end |
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
22 23 24 |
# File 'lib/mongo/cursor.rb', line 22 def fields @fields end |
#full_collection_name ⇒ Object (readonly)
Returns the value of attribute full_collection_name.
22 23 24 |
# File 'lib/mongo/cursor.rb', line 22 def full_collection_name @full_collection_name end |
#hint ⇒ Object (readonly)
Returns the value of attribute hint.
22 23 24 |
# File 'lib/mongo/cursor.rb', line 22 def hint @hint end |
#order ⇒ Object (readonly)
Returns the value of attribute order.
22 23 24 |
# File 'lib/mongo/cursor.rb', line 22 def order @order end |
#selector ⇒ Object (readonly)
Returns the value of attribute selector.
22 23 24 |
# File 'lib/mongo/cursor.rb', line 22 def selector @selector end |
#snapshot ⇒ Object (readonly)
Returns the value of attribute snapshot.
22 23 24 |
# File 'lib/mongo/cursor.rb', line 22 def snapshot @snapshot end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
22 23 24 |
# File 'lib/mongo/cursor.rb', line 22 def timeout @timeout end |
Instance Method Details
#close ⇒ True
Close the cursor.
Note: if a cursor is read until exhausted (read until Mongo::Constants::OP_QUERY or Mongo::Constants::OP_GETMORE returns zero for the cursor id), there is no need to close it manually.
Note also: Collection#find takes an optional block argument which can be used to ensure that your cursors get closed.
221 222 223 224 225 226 227 228 229 230 |
# File 'lib/mongo/cursor.rb', line 221 def close if @cursor_id = ByteBuffer.new([0, 0, 0, 0]) .put_int(1) .put_long(@cursor_id) @connection.(Mongo::Constants::OP_KILL_CURSORS, , "cursor.close()") end @cursor_id = 0 @closed = true end |
#closed? ⇒ Boolean
Is this cursor closed?
235 |
# File 'lib/mongo/cursor.rb', line 235 def closed?; @closed; end |
#count ⇒ Integer
Get the size of the result set for this query.
87 88 89 90 91 92 93 94 95 |
# File 'lib/mongo/cursor.rb', line 87 def count command = OrderedHash["count", @collection.name, "query", @selector, "fields", @fields] response = @db.command(command) return response['n'].to_i if response['ok'] == 1 return 0 if response['errmsg'] == "ns missing" raise OperationFailure, "Count failed: #{response['errmsg']}" end |
#each { ... } ⇒ Object
Iterate over each document in this cursor, yielding it to the given block.
Iterating over an entire cursor will close it.
170 171 172 173 174 175 176 |
# File 'lib/mongo/cursor.rb', line 170 def each num_returned = 0 while more? && (@limit <= 0 || num_returned < @limit) yield next_document num_returned += 1 end end |
#explain ⇒ Hash
Get the explain plan for this cursor.
203 204 205 206 207 208 209 |
# File 'lib/mongo/cursor.rb', line 203 def explain c = Cursor.new(@collection, .merge(:limit => -@limit.abs, :explain => true)) explanation = c.next_document c.close explanation end |
#limit(number_to_return = nil) ⇒ Integer
Limit the number of results to be returned by this cursor.
This method overrides any limit specified in the Collection#find method, and only the last limit applied has an effect.
132 133 134 135 136 137 138 139 |
# File 'lib/mongo/cursor.rb', line 132 def limit(number_to_return=nil) return @limit unless number_to_return check_modifiable raise ArgumentError, "limit requires an integer" unless number_to_return.is_a? Integer @limit = number_to_return self end |
#next_document ⇒ Hash, Nil
Get the next document specified the cursor options.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/mongo/cursor.rb', line 60 def next_document refill_via_get_more if num_remaining == 0 doc = @cache.shift if doc && doc['$err'] err = doc['$err'] # If the server has stopped being the master (e.g., it's one of a # pair but it has died or something like that) then we close that # connection. The next request will re-open on master server. if err == "not master" raise ConnectionFailure, err @connection.close end raise OperationFailure, err end doc end |
#query_options_hash ⇒ Hash
Get the query options for this Cursor.
252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/mongo/cursor.rb', line 252 def { :selector => @selector, :fields => @fields, :admin => @admin, :skip => @skip_num, :limit => @limit_num, :order => @order, :hint => @hint, :snapshot => @snapshot, :timeout => @timeout } end |
#query_opts ⇒ Integer
Returns an integer indicating which query options have been selected.
The MongoDB wire protocol.
243 244 245 246 247 |
# File 'lib/mongo/cursor.rb', line 243 def query_opts timeout = @timeout ? 0 : Mongo::Constants::OP_QUERY_NO_CURSOR_TIMEOUT slave_ok = @connection.slave_ok? ? Mongo::Constants::OP_QUERY_SLAVE_OK : 0 slave_ok + timeout end |
#skip(number_to_skip = nil) ⇒ Integer
Skips the first number_to_skip
results of this cursor. Returns the current number_to_skip if no parameter is given.
This method overrides any skip specified in the Collection#find method, and only the last skip applied has an effect.
150 151 152 153 154 155 156 157 |
# File 'lib/mongo/cursor.rb', line 150 def skip(number_to_skip=nil) return @skip unless number_to_skip check_modifiable raise ArgumentError, "skip requires an integer" unless number_to_skip.is_a? Integer @skip = number_to_skip self end |
#sort(key_or_list, direction = nil) ⇒ Object
Sort this cursor’s results.
This method overrides any sort order specified in the Collection#find method, and only the last sort applied has an effect.
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/mongo/cursor.rb', line 109 def sort(key_or_list, direction=nil) check_modifiable if !direction.nil? order = [[key_or_list, direction]] else order = key_or_list end @order = order self end |
#to_a ⇒ Array
Receive all the documents from this cursor as an array of hashes.
Note: use of this method is discouraged - in most cases, it’s much more efficient to retrieve documents as you need them by iterating over the cursor.
187 188 189 190 191 192 193 194 195 196 |
# File 'lib/mongo/cursor.rb', line 187 def to_a raise InvalidOperation, "can't call Cursor#to_a on a used cursor" if @query_run rows = [] num_returned = 0 while more? && (@limit <= 0 || num_returned < @limit) rows << next_document num_returned += 1 end rows end |