Class: Mongo::Operation::Result
- Inherits:
-
Object
- Object
- Mongo::Operation::Result
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/mongo/operation/result.rb,
lib/mongo/operation/shared/result/aggregatable.rb
Overview
Result wrapper for wire protocol replies.
An operation has zero or one replies. The only operations producing zero replies are unacknowledged writes; all other operations produce one reply. This class provides an object that can be operated on (for example, to check whether an operation succeeded) even when the operation did not produce a reply (in which case it is assumed to have succeeded).
Direct Known Subclasses
Aggregate::Result, CollectionsInfo::Result, Delete::BulkResult, Delete::Result, Explain::Result, Find::Result, GetMore::Result, Indexes::Result, Insert::BulkResult, Insert::Result, ListCollections::Result, MapReduce::Result, ParallelScan::Result, Update::BulkResult, Update::Result, UsersInfo::Result
Defined Under Namespace
Modules: Aggregatable
Constant Summary collapse
- CURSOR =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
The field name for the cursor document in an aggregation.
'cursor'- CURSOR_ID =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
The cursor id field in the cursor document.
'id'- FIRST_BATCH =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
The field name for the first batch of a cursor.
'firstBatch'- NEXT_BATCH =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
The field name for the next batch of a cursor.
'nextBatch'- NAMESPACE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
The namespace field in the cursor document.
'ns'- N =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
The number of documents updated in the write.
'n'- OK =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
The ok status field in the result.
'ok'- RESULT =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
The result field constant.
'result'
Instance Attribute Summary collapse
- #connection ⇒ Object readonly
-
#connection_description ⇒ Server::Description
readonly
private
Server description of the server that the operation was performed on that this result is for.
-
#connection_global_id ⇒ Object
readonly
private
Global is of the connection that the operation was performed on that this result is for.
-
#context ⇒ Operation::Context | nil
readonly
private
The operation context (if any) that was active when this result was produced.
-
#replies ⇒ Array<Protocol::Message>
readonly
private
Replies The wrapped wire protocol replies.
Instance Method Summary collapse
-
#acknowledged? ⇒ true, false
Is the result acknowledged?.
-
#cluster_time ⇒ ClusterTime | nil
Get the cluster time reported in the server response.
-
#cursor_id ⇒ Integer
private
Get the cursor id if the response is acknowledged.
-
#documents ⇒ Array<BSON::Document>
Get the documents in the result.
-
#each {|Each| ... } ⇒ Enumerator
Iterate over the documents in the replies.
-
#error ⇒ Error::OperationFailure::Family
private
The exception instance (of Error::OperationFailure::Family) that would be raised during processing of this result.
-
#has_cursor_id? ⇒ true, false
private
Whether the result contains cursor_id.
-
#initialize(replies, connection_description = nil, connection_global_id = nil, context: nil, connection: nil) ⇒ Result
constructor
private
Initialize a new result.
-
#inspect ⇒ String
Get the pretty formatted inspection of the result.
-
#labels ⇒ Array
private
Gets the set of error labels associated with the result.
-
#namespace ⇒ Nil
private
Get the namespace of the cursor.
-
#ok? ⇒ true, false
Check the first document’s ok field.
-
#operation_time ⇒ Object | nil
Get the operation time reported in the server response.
-
#reply ⇒ Protocol::Message
private
Get the reply from the result.
-
#returned_count ⇒ Integer
Get the number of documents returned by the server in this batch.
- #snapshot_timestamp ⇒ Object
-
#successful? ⇒ true, false
If the result was a command then determine if it was considered a success.
-
#topology_version ⇒ TopologyVersion | nil
private
The topology version.
-
#validate! ⇒ Result
private
Validate the result by checking for any errors.
-
#write_concern_error? ⇒ Boolean
private
Whether the operation failed with a write concern error.
-
#written_count ⇒ Integer
(also: #n)
Get the number of documents written by the server.
Constructor Details
#initialize(replies, connection_description = nil, connection_global_id = nil, context: nil, connection: nil) ⇒ Result
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initialize a new result.
For an unkacknowledged write, pass nil in replies.
For all other operations, replies must be a Protocol::Message instance or an array containing a single Protocol::Message instance.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/mongo/operation/result.rb', line 104 def initialize(replies, connection_description = nil, connection_global_id = nil, context: nil, connection: nil) @context = context return unless replies if replies.is_a?(Array) raise ArgumentError, "Only one (or zero) reply is supported, given #{replies.length}" if replies.length != 1 reply = replies.first else reply = replies end unless reply.is_a?(Protocol::Message) raise ArgumentError, "Argument must be a Message instance, but is a #{reply.class}: #{reply.inspect}" end @replies = [ reply ] @connection_description = connection_description @connection_global_id = connection_global_id @connection = connection end |
Instance Attribute Details
#connection ⇒ Object (readonly)
149 150 151 |
# File 'lib/mongo/operation/result.rb', line 149 def connection @connection end |
#connection_description ⇒ Server::Description (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns Server description of the server that the operation was performed on that this result is for.
135 136 137 |
# File 'lib/mongo/operation/result.rb', line 135 def connection_description @connection_description end |
#connection_global_id ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns Global is of the connection that the operation was performed on that this result is for.
141 142 143 |
# File 'lib/mongo/operation/result.rb', line 141 def connection_global_id @connection_global_id end |
#context ⇒ Operation::Context | nil (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the operation context (if any) that was active when this result was produced.
147 148 149 |
# File 'lib/mongo/operation/result.rb', line 147 def context @context end |
#replies ⇒ Array<Protocol::Message> (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns replies The wrapped wire protocol replies.
129 130 131 |
# File 'lib/mongo/operation/result.rb', line 129 def replies @replies end |
Instance Method Details
#acknowledged? ⇒ true, false
Is the result acknowledged?
161 162 163 |
# File 'lib/mongo/operation/result.rb', line 161 def acknowledged? !!@replies end |
#cluster_time ⇒ ClusterTime | nil
Get the cluster time reported in the server response.
Changed in version 2.9.0: This attribute became an instance of ClusterTime, which is a subclass of BSON::Document. Previously it was an instance of BSON::Document.
427 428 429 |
# File 'lib/mongo/operation/result.rb', line 427 def cluster_time first_document && ClusterTime[first_document['$clusterTime']] end |
#cursor_id ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Cursor ids of 0 indicate there is no cursor on the server.
Get the cursor id if the response is acknowledged.
185 186 187 |
# File 'lib/mongo/operation/result.rb', line 185 def cursor_id acknowledged? ? replies.last.cursor_id : 0 end |
#documents ⇒ Array<BSON::Document>
Get the documents in the result.
209 210 211 212 213 214 215 |
# File 'lib/mongo/operation/result.rb', line 209 def documents if acknowledged? replies.flat_map(&:documents) else [] end end |
#each {|Each| ... } ⇒ Enumerator
Iterate over the documents in the replies.
230 231 232 |
# File 'lib/mongo/operation/result.rb', line 230 def each(&block) documents.each(&block) end |
#error ⇒ Error::OperationFailure::Family
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The exception instance (of Error::OperationFailure::Family) that would be raised during processing of this result.
This method should only be called when result is not successful.
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
# File 'lib/mongo/operation/result.rb', line 344 def error @error ||= operation_failure_class.new( parser., self, code: parser.code, code_name: parser.code_name, write_concern_error_document: parser.write_concern_error_document, write_concern_error_code: parser.write_concern_error_code, write_concern_error_code_name: parser.write_concern_error_code_name, write_concern_error_labels: parser.write_concern_error_labels, labels: parser.labels, wtimeout: parser.wtimeout, connection_description: connection_description, document: parser.document, server_message: parser. ) end |
#has_cursor_id? ⇒ true, false
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Whether the result contains cursor_id
170 171 172 |
# File 'lib/mongo/operation/result.rb', line 170 def has_cursor_id? acknowledged? && replies.last.respond_to?(:cursor_id) end |
#inspect ⇒ String
Get the pretty formatted inspection of the result.
243 244 245 |
# File 'lib/mongo/operation/result.rb', line 243 def inspect "#<#{self.class.name}:0x#{object_id} documents=#{documents}>" end |
#labels ⇒ Array
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Gets the set of error labels associated with the result.
440 441 442 |
# File 'lib/mongo/operation/result.rb', line 440 def labels @labels ||= parser.labels end |
#namespace ⇒ Nil
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get the namespace of the cursor. The method should be defined in result classes where ‘ns’ is in the server response.
196 197 198 |
# File 'lib/mongo/operation/result.rb', line 196 def namespace nil end |
#ok? ⇒ true, false
Check the first document’s ok field.
308 309 310 311 312 313 314 315 |
# File 'lib/mongo/operation/result.rb', line 308 def ok? # first_document[OK] is a float, and the server can return # ok as a BSON int32, BSON int64 or a BSON double. # The number 1 is exactly representable in a float, hence # 1.0 == 1 is going to perform correctly all of the time # (until the server returns something other than 1 for success, that is) first_document[OK] == 1 end |
#operation_time ⇒ Object | nil
Get the operation time reported in the server response.
410 411 412 |
# File 'lib/mongo/operation/result.rb', line 410 def operation_time first_document && first_document[OPERATION_TIME] end |
#reply ⇒ Protocol::Message
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get the reply from the result.
Returns nil if there is no reply (i.e. the operation was an unacknowledged write).
256 257 258 259 260 |
# File 'lib/mongo/operation/result.rb', line 256 def reply return unless acknowledged? replies.first end |
#returned_count ⇒ Integer
Get the number of documents returned by the server in this batch.
268 269 270 271 272 273 274 |
# File 'lib/mongo/operation/result.rb', line 268 def returned_count if acknowledged? reply.number_returned else 0 end end |
#snapshot_timestamp ⇒ Object
451 452 453 454 455 |
# File 'lib/mongo/operation/result.rb', line 451 def return unless doc = reply.documents.first doc['cursor']&.[]('atClusterTime') || doc['atClusterTime'] end |
#successful? ⇒ true, false
If the write was unacknowledged, then this will always return true.
If the result was a command then determine if it was considered a success.
289 290 291 292 293 294 295 296 297 |
# File 'lib/mongo/operation/result.rb', line 289 def successful? return true unless acknowledged? if first_document.has_key?(OK) ok? && parser..empty? else !query_failure? && parser..empty? end end |
#topology_version ⇒ TopologyVersion | nil
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns The topology version.
373 374 375 376 377 378 379 |
# File 'lib/mongo/operation/result.rb', line 373 def topology_version unless defined?(@topology_version) @topology_version = first_document['topologyVersion'] && TopologyVersion.new(first_document['topologyVersion']) end @topology_version end |
#validate! ⇒ Result
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This only checks for errors with writes since authentication is handled at the connection level and any authentication errors would be raised there, before a Result is ever created.
Validate the result by checking for any errors.
332 333 334 |
# File 'lib/mongo/operation/result.rb', line 332 def validate! successful? ? self : raise_operation_failure end |
#write_concern_error? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Whether the operation failed with a write concern error.
447 448 449 |
# File 'lib/mongo/operation/result.rb', line 447 def write_concern_error? !!(first_document && first_document['writeConcernError']) end |
#written_count ⇒ Integer Also known as: n
Get the number of documents written by the server.
390 391 392 393 394 395 396 |
# File 'lib/mongo/operation/result.rb', line 390 def written_count if acknowledged? first_document[N] || 0 else 0 end end |