Class: Mongo::Operation::Result
- Inherits:
-
Object
- Object
- Mongo::Operation::Result
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/mongo/operation/result.rb
Overview
Result wrapper for operations.
Direct Known Subclasses
Commands::Aggregate::Result, Commands::CollectionsInfo::Result, Commands::Find::Result, Commands::GetMore::Result, Commands::ListCollections::Result, Commands::ListIndexes::Result, Commands::MapReduce::Result, Commands::ParallelScan::Result, Commands::UsersInfo::Result, Mongo::Operation::Read::Query::Result, Write::Bulk::Delete::LegacyResult, Write::Bulk::Delete::Result, Write::Bulk::Insert::LegacyResult, Write::Bulk::Insert::Result, Write::Bulk::Update::LegacyResult, Write::Bulk::Update::Result, Write::Delete::Result, Write::Insert::Result, Write::Update::LegacyResult, Write::Update::Result
Constant Summary collapse
- CURSOR =
The field name for the cursor document in an aggregation.
'cursor'.freeze
- CURSOR_ID =
The cursor id field in the cursor document.
'id'.freeze
- FIRST_BATCH =
The field name for the first batch of a cursor.
'firstBatch'.freeze
- NEXT_BATCH =
The field name for the next batch of a cursor.
'nextBatch'.freeze
- NAMESPACE =
The namespace field in the cursor document.
'ns'.freeze
- N =
The number of documents updated in the write.
'n'.freeze
- OK =
The ok status field in the result.
'ok'.freeze
- RESULT =
The result field constant.
'result'.freeze
Instance Attribute Summary collapse
-
#replies ⇒ Array<Protocol::Reply>
readonly
Replies The wrapped wire protocol replies.
Instance Method Summary collapse
-
#acknowledged? ⇒ true, false
Is the result acknowledged?.
-
#cursor_id ⇒ Integer
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.
-
#initialize(replies) ⇒ Result
constructor
Initialize a new result.
-
#inspect ⇒ String
Get the pretty formatted inspection of the result.
-
#multiple? ⇒ true, false
Determine if this result is a collection of multiple replies from the server.
-
#namespace ⇒ Nil
Get the namespace of the cursor.
-
#ok? ⇒ true, false
Check the first document’s ok field.
-
#reply ⇒ Protocol::Reply
Get the first reply from the result.
-
#returned_count ⇒ Integer
Get the count of documents returned by the server.
-
#successful? ⇒ true, false
If the result was a command then determine if it was considered a success.
-
#validate! ⇒ Result
Validate the result by checking for any errors.
-
#written_count ⇒ Integer
(also: #n)
Get the number of documents written by the server.
Constructor Details
Instance Attribute Details
#replies ⇒ Array<Protocol::Reply> (readonly)
Returns replies The wrapped wire protocol replies.
66 67 68 |
# File 'lib/mongo/operation/result.rb', line 66 def replies @replies end |
Instance Method Details
#acknowledged? ⇒ true, false
On MongoDB 2.6 and higher all writes are acknowledged since the driver uses write commands for all write operations. On 2.4 and lower, the result is acknowledged if the GLE has been executed after the command. If not, no replies will be specified. Reads will always return true here since a replies is always provided.
Is the result acknowledged?
79 80 81 |
# File 'lib/mongo/operation/result.rb', line 79 def acknowledged? !!@replies end |
#cursor_id ⇒ Integer
Cursor ids of 0 indicate there is no cursor on the server.
Get the cursor id if the response is acknowledged.
106 107 108 |
# File 'lib/mongo/operation/result.rb', line 106 def cursor_id acknowledged? ? replies.last.cursor_id : 0 end |
#documents ⇒ Array<BSON::Document>
Get the documents in the result.
128 129 130 131 132 133 134 |
# File 'lib/mongo/operation/result.rb', line 128 def documents if acknowledged? replies.flat_map{ |reply| reply.documents } else [] end end |
#each {|Each| ... } ⇒ Enumerator
Iterate over the documents in the replies.
148 149 150 |
# File 'lib/mongo/operation/result.rb', line 148 def each(&block) documents.each(&block) end |
#inspect ⇒ String
Get the pretty formatted inspection of the result.
172 173 174 |
# File 'lib/mongo/operation/result.rb', line 172 def inspect "#<Mongo::Operation::Result:#{object_id} documents=#{documents}>" end |
#multiple? ⇒ true, false
Determine if this result is a collection of multiple replies from the server.
92 93 94 |
# File 'lib/mongo/operation/result.rb', line 92 def multiple? replies.size > 1 end |
#namespace ⇒ Nil
Get the namespace of the cursor. The method should be defined in result classes where ‘ns’ is in the server response.
116 117 118 |
# File 'lib/mongo/operation/result.rb', line 116 def namespace nil end |
#ok? ⇒ true, false
Check the first document’s ok field.
237 238 239 |
# File 'lib/mongo/operation/result.rb', line 237 def ok? first_document[OK] == 1 end |
#reply ⇒ Protocol::Reply
Get the first reply from the result.
184 185 186 187 188 189 190 |
# File 'lib/mongo/operation/result.rb', line 184 def reply if acknowledged? replies.first else nil end end |
#returned_count ⇒ Integer
Get the count of documents returned by the server.
200 201 202 203 204 205 206 |
# File 'lib/mongo/operation/result.rb', line 200 def returned_count if acknowledged? multiple? ? aggregate_returned_count : reply.number_returned else 0 end 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.
220 221 222 223 224 225 226 227 |
# File 'lib/mongo/operation/result.rb', line 220 def successful? return true if !acknowledged? if first_document.has_key?(OK) ok? && parser..empty? else !query_failure? && parser..empty? end end |
#validate! ⇒ Result
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.
255 256 257 |
# File 'lib/mongo/operation/result.rb', line 255 def validate! !successful? ? raise(Error::OperationFailure.new(parser.)) : self end |
#written_count ⇒ Integer Also known as: n
Get the number of documents written by the server.
267 268 269 270 271 272 273 |
# File 'lib/mongo/operation/result.rb', line 267 def written_count if acknowledged? multiple? ? aggregate_written_count : (first_document[N] || 0) else 0 end end |