mongo 2.4.3 → 2.5.0.beta

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (235) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +3 -2
  3. data.tar.gz.sig +0 -0
  4. data/lib/mongo.rb +3 -2
  5. data/lib/mongo/auth/cr.rb +6 -4
  6. data/lib/mongo/auth/cr/conversation.rb +33 -17
  7. data/lib/mongo/auth/ldap.rb +4 -2
  8. data/lib/mongo/auth/ldap/conversation.rb +19 -9
  9. data/lib/mongo/auth/scram.rb +7 -4
  10. data/lib/mongo/auth/scram/conversation.rb +62 -24
  11. data/lib/mongo/auth/user.rb +10 -0
  12. data/lib/mongo/auth/user/view.rb +44 -22
  13. data/lib/mongo/auth/x509.rb +4 -2
  14. data/lib/mongo/auth/x509/conversation.rb +19 -9
  15. data/lib/mongo/bulk_write.rb +33 -27
  16. data/lib/mongo/bulk_write/combineable.rb +5 -0
  17. data/lib/mongo/bulk_write/transformable.rb +2 -0
  18. data/lib/mongo/bulk_write/validatable.rb +4 -0
  19. data/lib/mongo/client.rb +123 -12
  20. data/lib/mongo/cluster.rb +52 -11
  21. data/lib/mongo/cluster/app_metadata.rb +8 -2
  22. data/lib/mongo/cluster/cursor_reaper.rb +0 -1
  23. data/lib/mongo/cluster/topology.rb +1 -1
  24. data/lib/mongo/collection.rb +114 -27
  25. data/lib/mongo/collection/view.rb +8 -2
  26. data/lib/mongo/collection/view/aggregation.rb +11 -7
  27. data/lib/mongo/collection/view/builder/aggregation.rb +5 -1
  28. data/lib/mongo/collection/view/builder/find_command.rb +5 -3
  29. data/lib/mongo/collection/view/builder/map_reduce.rb +11 -3
  30. data/lib/mongo/collection/view/builder/op_query.rb +1 -1
  31. data/lib/mongo/collection/view/change_stream.rb +160 -0
  32. data/lib/mongo/collection/view/change_stream/retryable.rb +57 -0
  33. data/lib/mongo/collection/view/iterable.rb +11 -10
  34. data/lib/mongo/collection/view/map_reduce.rb +22 -18
  35. data/lib/mongo/collection/view/readable.rb +51 -37
  36. data/lib/mongo/collection/view/writable.rb +72 -40
  37. data/lib/mongo/cursor.rb +25 -4
  38. data/lib/mongo/cursor/builder/get_more_command.rb +4 -2
  39. data/lib/mongo/database.rb +22 -11
  40. data/lib/mongo/database/view.rb +16 -12
  41. data/lib/mongo/error.rb +5 -0
  42. data/lib/mongo/error/invalid_session.rb +36 -0
  43. data/lib/mongo/error/missing_resume_token.rb +39 -0
  44. data/lib/mongo/error/operation_failure.rb +17 -0
  45. data/lib/mongo/error/parser.rb +3 -2
  46. data/lib/mongo/error/unknown_payload_type.rb +41 -0
  47. data/lib/mongo/error/unsupported_array_filters.rb +51 -0
  48. data/lib/mongo/error/unsupported_message_type.rb +23 -0
  49. data/lib/mongo/grid/fs_bucket.rb +5 -4
  50. data/lib/mongo/grid/stream/read.rb +3 -2
  51. data/lib/mongo/grid/stream/write.rb +2 -2
  52. data/lib/mongo/index/view.rb +35 -25
  53. data/lib/mongo/monitoring/event/secure.rb +14 -0
  54. data/lib/mongo/operation.rb +16 -0
  55. data/lib/mongo/operation/commands.rb +1 -0
  56. data/lib/mongo/operation/commands/aggregate.rb +9 -5
  57. data/lib/mongo/operation/commands/aggregate/result.rb +1 -1
  58. data/lib/mongo/operation/commands/collections_info.rb +6 -6
  59. data/lib/mongo/operation/commands/command.rb +2 -1
  60. data/lib/mongo/operation/commands/create.rb +6 -2
  61. data/lib/mongo/operation/commands/drop.rb +6 -2
  62. data/lib/mongo/operation/commands/drop_database.rb +6 -2
  63. data/lib/mongo/operation/commands/explain.rb +27 -0
  64. data/lib/mongo/operation/commands/explain/result.rb +52 -0
  65. data/lib/mongo/operation/commands/indexes.rb +1 -1
  66. data/lib/mongo/operation/commands/list_collections.rb +1 -1
  67. data/lib/mongo/operation/commands/list_collections/result.rb +1 -1
  68. data/lib/mongo/operation/commands/list_indexes.rb +1 -1
  69. data/lib/mongo/operation/commands/list_indexes/result.rb +1 -1
  70. data/lib/mongo/operation/commands/map_reduce.rb +8 -4
  71. data/lib/mongo/operation/commands/map_reduce/result.rb +13 -1
  72. data/lib/mongo/operation/commands/user_query.rb +1 -1
  73. data/lib/mongo/operation/commands/users_info.rb +6 -2
  74. data/lib/mongo/operation/executable.rb +4 -1
  75. data/lib/mongo/operation/read_preference.rb +10 -5
  76. data/lib/mongo/operation/result.rb +26 -2
  77. data/lib/mongo/operation/specifiable.rb +13 -1
  78. data/lib/mongo/operation/uses_command_op_msg.rb +47 -0
  79. data/lib/mongo/operation/write/bulk/bulkable.rb +4 -1
  80. data/lib/mongo/operation/write/bulk/insert/result.rb +4 -4
  81. data/lib/mongo/operation/write/command/create_index.rb +6 -1
  82. data/lib/mongo/operation/write/command/delete.rb +28 -4
  83. data/lib/mongo/operation/write/command/drop_index.rb +6 -1
  84. data/lib/mongo/operation/write/command/insert.rb +22 -18
  85. data/lib/mongo/operation/write/command/update.rb +24 -9
  86. data/lib/mongo/operation/write/command/writable.rb +14 -1
  87. data/lib/mongo/operation/write/insert.rb +4 -1
  88. data/lib/mongo/operation/write/insert/result.rb +2 -2
  89. data/lib/mongo/operation/write/update.rb +7 -1
  90. data/lib/mongo/operation/write/write_command_enabled.rb +20 -3
  91. data/lib/mongo/protocol.rb +3 -0
  92. data/lib/mongo/protocol/bit_vector.rb +2 -2
  93. data/lib/mongo/protocol/compressed.rb +135 -0
  94. data/lib/mongo/protocol/delete.rb +8 -6
  95. data/lib/mongo/protocol/get_more.rb +8 -6
  96. data/lib/mongo/protocol/insert.rb +8 -6
  97. data/lib/mongo/protocol/kill_cursors.rb +8 -6
  98. data/lib/mongo/protocol/message.rb +31 -3
  99. data/lib/mongo/protocol/msg.rb +172 -0
  100. data/lib/mongo/protocol/query.rb +26 -6
  101. data/lib/mongo/protocol/registry.rb +76 -0
  102. data/lib/mongo/protocol/reply.rb +10 -5
  103. data/lib/mongo/protocol/serializers.rb +224 -0
  104. data/lib/mongo/protocol/update.rb +8 -6
  105. data/lib/mongo/retryable.rb +4 -2
  106. data/lib/mongo/server.rb +6 -3
  107. data/lib/mongo/server/connectable.rb +1 -1
  108. data/lib/mongo/server/connection.rb +30 -8
  109. data/lib/mongo/server/description.rb +25 -1
  110. data/lib/mongo/server/description/features.rb +4 -1
  111. data/lib/mongo/server/monitor.rb +5 -0
  112. data/lib/mongo/server/monitor/connection.rb +50 -2
  113. data/lib/mongo/server_selector/nearest.rb +10 -4
  114. data/lib/mongo/server_selector/primary.rb +20 -0
  115. data/lib/mongo/server_selector/primary_preferred.rb +10 -4
  116. data/lib/mongo/server_selector/secondary.rb +10 -4
  117. data/lib/mongo/server_selector/secondary_preferred.rb +24 -4
  118. data/lib/mongo/session.rb +180 -0
  119. data/lib/mongo/session/server_session.rb +73 -0
  120. data/lib/mongo/session/session_pool.rb +161 -0
  121. data/lib/mongo/uri.rb +11 -0
  122. data/lib/mongo/version.rb +1 -1
  123. data/mongo.gemspec +2 -1
  124. data/spec/mongo/auth/cr_spec.rb +12 -0
  125. data/spec/mongo/auth/ldap_spec.rb +2 -0
  126. data/spec/mongo/auth/scram/conversation_spec.rb +6 -6
  127. data/spec/mongo/auth/scram_spec.rb +25 -1
  128. data/spec/mongo/auth/user/view_spec.rb +268 -76
  129. data/spec/mongo/auth/x509_spec.rb +2 -0
  130. data/spec/mongo/bulk_write_spec.rb +435 -5
  131. data/spec/mongo/client_spec.rb +356 -39
  132. data/spec/mongo/cluster/app_metadata_spec.rb +2 -2
  133. data/spec/mongo/cluster_spec.rb +176 -0
  134. data/spec/mongo/collection/view/aggregation_spec.rb +33 -12
  135. data/spec/mongo/collection/view/builder/find_command_spec.rb +46 -6
  136. data/spec/mongo/collection/view/change_stream_spec.rb +814 -0
  137. data/spec/mongo/collection/view/map_reduce_spec.rb +94 -17
  138. data/spec/mongo/collection/view/readable_spec.rb +3 -12
  139. data/spec/mongo/collection_spec.rb +1048 -42
  140. data/spec/mongo/cursor/builder/get_more_command_spec.rb +19 -0
  141. data/spec/mongo/cursor_spec.rb +2 -2
  142. data/spec/mongo/database_spec.rb +50 -1
  143. data/spec/mongo/grid/fs_bucket_spec.rb +225 -137
  144. data/spec/mongo/grid/stream/read_spec.rb +2 -2
  145. data/spec/mongo/index/view_spec.rb +146 -8
  146. data/spec/mongo/monitoring/event/secure_spec.rb +42 -0
  147. data/spec/mongo/operation/read/query_spec.rb +2 -1
  148. data/spec/mongo/operation/specifiable_spec.rb +2 -2
  149. data/spec/mongo/operation/write/command/delete_spec.rb +96 -13
  150. data/spec/mongo/operation/write/command/insert_spec.rb +111 -12
  151. data/spec/mongo/operation/write/command/update_spec.rb +93 -10
  152. data/spec/mongo/operation/write/delete_spec.rb +1 -1
  153. data/spec/mongo/operation/write/insert_spec.rb +1 -1
  154. data/spec/mongo/operation/write/update_spec.rb +1 -1
  155. data/spec/mongo/protocol/compressed_spec.rb +66 -0
  156. data/spec/mongo/protocol/delete_spec.rb +14 -0
  157. data/spec/mongo/protocol/get_more_spec.rb +14 -0
  158. data/spec/mongo/protocol/insert_spec.rb +14 -0
  159. data/spec/mongo/protocol/kill_cursors_spec.rb +14 -0
  160. data/spec/mongo/protocol/msg_spec.rb +499 -0
  161. data/spec/mongo/protocol/query_spec.rb +45 -0
  162. data/spec/mongo/protocol/registry_spec.rb +31 -0
  163. data/spec/mongo/protocol/reply_spec.rb +14 -0
  164. data/spec/mongo/protocol/update_spec.rb +14 -0
  165. data/spec/mongo/retryable_spec.rb +6 -2
  166. data/spec/mongo/sdam_spec.rb +4 -0
  167. data/spec/mongo/server/connection_spec.rb +4 -2
  168. data/spec/mongo/server/description_spec.rb +28 -1
  169. data/spec/mongo/session/server_session_spec.rb +16 -0
  170. data/spec/mongo/session/session_pool_spec.rb +194 -0
  171. data/spec/mongo/uri_spec.rb +31 -2
  172. data/spec/spec_helper.rb +104 -0
  173. data/spec/support/authorization.rb +6 -1
  174. data/spec/support/crud.rb +3 -1
  175. data/spec/support/crud/write.rb +6 -1
  176. data/spec/support/crud_tests/write/findOneAndUpdate-arrayFilters.yml +69 -0
  177. data/spec/support/crud_tests/write/updateMany-arrayFilters.yml +63 -0
  178. data/spec/support/crud_tests/write/updateOne-arrayFilters.yml +109 -0
  179. data/spec/support/sdam/rs/discover_arbiters.yml +1 -1
  180. data/spec/support/sdam/rs/discover_passives.yml +2 -2
  181. data/spec/support/sdam/rs/discover_primary.yml +1 -1
  182. data/spec/support/sdam/rs/discover_secondary.yml +1 -1
  183. data/spec/support/sdam/rs/discovery.yml +4 -4
  184. data/spec/support/sdam/rs/equal_electionids.yml +1 -0
  185. data/spec/support/sdam/rs/ghost_discovered.yml +1 -1
  186. data/spec/support/sdam/rs/hosts_differ_from_seeds.yml +1 -1
  187. data/spec/support/sdam/rs/ls_timeout.yml +88 -0
  188. data/spec/support/sdam/rs/member_reconfig.yml +2 -2
  189. data/spec/support/sdam/rs/member_standalone.yml +2 -2
  190. data/spec/support/sdam/rs/new_primary.yml +2 -2
  191. data/spec/support/sdam/rs/new_primary_new_electionid.yml +3 -0
  192. data/spec/support/sdam/rs/new_primary_new_setversion.yml +3 -0
  193. data/spec/support/sdam/rs/new_primary_wrong_set_name.yml +2 -2
  194. data/spec/support/sdam/rs/non_rs_member.yml +1 -1
  195. data/spec/support/sdam/rs/normalize_case.yml +1 -1
  196. data/spec/support/sdam/rs/null_election_id.yml +4 -0
  197. data/spec/support/sdam/rs/primary_becomes_standalone.yml +2 -2
  198. data/spec/support/sdam/rs/primary_changes_set_name.yml +2 -2
  199. data/spec/support/sdam/rs/primary_disconnect.yml +2 -2
  200. data/spec/support/sdam/rs/primary_disconnect_electionid.yml +5 -0
  201. data/spec/support/sdam/rs/primary_disconnect_setversion.yml +5 -0
  202. data/spec/support/sdam/rs/primary_hint_from_secondary_with_mismatched_me.yml +58 -0
  203. data/spec/support/sdam/rs/primary_reports_new_member.yml +4 -4
  204. data/spec/support/sdam/rs/primary_to_no_primary_mismatched_me.yml +2 -2
  205. data/spec/support/sdam/rs/primary_wrong_set_name.yml +1 -1
  206. data/spec/support/sdam/rs/response_from_removed.yml +2 -2
  207. data/spec/support/sdam/rs/rsother_discovered.yml +1 -1
  208. data/spec/support/sdam/rs/sec_not_auth.yml +1 -1
  209. data/spec/support/sdam/rs/secondary_wrong_set_name.yml +1 -1
  210. data/spec/support/sdam/rs/secondary_wrong_set_name_with_primary.yml +2 -2
  211. data/spec/support/sdam/rs/setversion_without_electionid.yml +2 -0
  212. data/spec/support/sdam/rs/stepdown_change_set_name.yml +2 -2
  213. data/spec/support/sdam/rs/unexpected_mongos.yml +1 -1
  214. data/spec/support/sdam/rs/use_setversion_without_electionid.yml +3 -0
  215. data/spec/support/sdam/rs/wrong_set_name.yml +1 -1
  216. data/spec/support/sdam/sharded/ls_timeout_mongos.yml +97 -0
  217. data/spec/support/sdam/sharded/mongos_disconnect.yml +3 -3
  218. data/spec/support/sdam/sharded/multiple_mongoses.yml +1 -1
  219. data/spec/support/sdam/sharded/non_mongos_removed.yml +1 -1
  220. data/spec/support/sdam/sharded/normalize_uri_case.yml +1 -1
  221. data/spec/support/sdam/single/direct_connection_external_ip.yml +1 -1
  222. data/spec/support/sdam/single/direct_connection_mongos.yml +1 -1
  223. data/spec/support/sdam/single/direct_connection_rsarbiter.yml +1 -1
  224. data/spec/support/sdam/single/direct_connection_rsprimary.yml +1 -1
  225. data/spec/support/sdam/single/direct_connection_rssecondary.yml +1 -1
  226. data/spec/support/sdam/single/direct_connection_slave.yml +1 -1
  227. data/spec/support/sdam/single/direct_connection_standalone.yml +1 -1
  228. data/spec/support/sdam/single/ls_timeout_standalone.yml +35 -0
  229. data/spec/support/sdam/single/not_ok_response.yml +1 -1
  230. data/spec/support/sdam/single/standalone_removed.yml +1 -1
  231. data/spec/support/sdam/single/unavailable_seed.yml +1 -1
  232. data/spec/support/server_discovery_and_monitoring.rb +4 -0
  233. data/spec/support/shared/session.rb +236 -0
  234. metadata +53 -15
  235. metadata.gz.sig +0 -0
@@ -133,7 +133,7 @@ module Mongo
133
133
  #
134
134
  # @since 2.1.0
135
135
  def read_preference
136
- @read_preference ||= ServerSelector.get(@options[:read] || fs.read_preference)
136
+ @read_preference ||= options[:read] || fs.read_preference
137
137
  end
138
138
 
139
139
  # Get the files collection file information document for the file being read.
@@ -167,7 +167,8 @@ module Mongo
167
167
  end
168
168
 
169
169
  def view
170
- @view ||= fs.chunks_collection.find({ :files_id => file_id }, options).read(read_preference).sort(:n => 1)
170
+ @view ||= (opts = options.merge(read: read_preference) if read_preference
171
+ fs.chunks_collection.find({ :files_id => file_id }, opts || options).sort(:n => 1))
171
172
  end
172
173
 
173
174
  def validate!(index, num_chunks, chunk, length_read)
@@ -103,7 +103,7 @@ module Mongo
103
103
  def close
104
104
  ensure_open!
105
105
  update_length
106
- files_collection.insert_one(file_info)
106
+ files_collection.insert_one(file_info, @options)
107
107
  @open = false
108
108
  file_id
109
109
  end
@@ -142,7 +142,7 @@ module Mongo
142
142
  #
143
143
  # @since 2.1.0
144
144
  def abort
145
- fs.chunks_collection.find(:files_id => file_id).delete_many
145
+ fs.chunks_collection.find({ :files_id => file_id }, @options).delete_many
146
146
  @open = false || true
147
147
  end
148
148
 
@@ -29,7 +29,7 @@ module Mongo
29
29
  # when sending the listIndexes command.
30
30
  attr_reader :batch_size
31
31
 
32
- def_delegators :@collection, :cluster, :database, :read_preference, :write_concern
32
+ def_delegators :@collection, :cluster, :database, :read_preference, :write_concern, :client
33
33
  def_delegators :cluster, :next_primary
34
34
 
35
35
  # The index key field.
@@ -149,14 +149,16 @@ module Mongo
149
149
  # @since 2.0.0
150
150
  def create_many(*models)
151
151
  server = next_primary
152
- spec = {
153
- indexes: normalize_models(models.flatten, server),
154
- db_name: database.name,
155
- coll_name: collection.name
156
- }
157
-
158
- spec[:write_concern] = write_concern if server.features.collation_enabled?
159
- Operation::Write::CreateIndex.new(spec).execute(server)
152
+ client.send(:with_session, @options) do |session|
153
+ spec = {
154
+ indexes: normalize_models(models.flatten, server),
155
+ db_name: database.name,
156
+ coll_name: collection.name,
157
+ session: session
158
+ }
159
+ spec[:write_concern] = write_concern if server.features.collation_enabled?
160
+ Operation::Write::CreateIndex.new(spec).execute(server)
161
+ end
160
162
  end
161
163
 
162
164
  # Convenience method for getting index information by a specific name or
@@ -189,11 +191,13 @@ module Mongo
189
191
  # @since 2.0.0
190
192
  def each(&block)
191
193
  server = next_primary(false)
192
- cursor = Cursor.new(self, send_initial_query(server), server).to_enum
194
+ session = client.send(:get_session, @options)
195
+ result = send_initial_query(server, session)
196
+ cursor = Cursor.new(self, result, server, session: session)
193
197
  cursor.each do |doc|
194
198
  yield doc
195
199
  end if block_given?
196
- cursor
200
+ cursor.to_enum
197
201
  end
198
202
 
199
203
  # Create the new index view.
@@ -213,35 +217,41 @@ module Mongo
213
217
  def initialize(collection, options = {})
214
218
  @collection = collection
215
219
  @batch_size = options[:batch_size]
220
+ @options = options
216
221
  end
217
222
 
218
223
  private
219
224
 
220
225
  def drop_by_name(name)
221
- spec = {
222
- db_name: database.name,
223
- coll_name: collection.name,
224
- index_name: name
225
- }
226
- server = next_primary
227
- spec[:write_concern] = write_concern if server.features.collation_enabled?
228
- Operation::Write::DropIndex.new(spec).execute(server)
226
+ client.send(:with_session, @options) do |session|
227
+ spec = {
228
+ db_name: database.name,
229
+ coll_name: collection.name,
230
+ index_name: name,
231
+ session: session
232
+ }
233
+ server = next_primary
234
+ spec[:write_concern] = write_concern if server.features.collation_enabled?
235
+ Operation::Write::DropIndex.new(spec).execute(server)
236
+ end
229
237
  end
230
238
 
231
239
  def index_name(spec)
232
240
  spec.to_a.join('_')
233
241
  end
234
242
 
235
- def indexes_spec
243
+ def indexes_spec(session)
236
244
  { selector: {
237
245
  listIndexes: collection.name,
238
246
  cursor: batch_size ? { batchSize: batch_size } : {} },
239
247
  coll_name: collection.name,
240
- db_name: database.name }
248
+ db_name: database.name,
249
+ session: session
250
+ }
241
251
  end
242
252
 
243
- def initial_query_op
244
- Operation::Commands::Indexes.new(indexes_spec)
253
+ def initial_query_op(session)
254
+ Operation::Commands::Indexes.new(indexes_spec(session))
245
255
  end
246
256
 
247
257
  def limit; -1; end
@@ -257,8 +267,8 @@ module Mongo
257
267
  end
258
268
  end
259
269
 
260
- def send_initial_query(server)
261
- initial_query_op.execute(server)
270
+ def send_initial_query(server, session)
271
+ initial_query_op(session).execute(server)
262
272
  end
263
273
 
264
274
  def with_generated_names(models, server)
@@ -52,6 +52,20 @@ module Mongo
52
52
  def redacted(command_name, document)
53
53
  REDACTED_COMMANDS.include?(command_name.to_s) ? BSON::Document.new : document
54
54
  end
55
+
56
+ # Is compression allowed for a given command message.
57
+ #
58
+ # @example Determine if compression is allowed for a given command.
59
+ # secure.compression_allowed?(selector)
60
+ #
61
+ # @param [ String, Symbol ] command_name The command name.
62
+ #
63
+ # @return [ true, false ] Whether compression can be used.
64
+ #
65
+ # @since 2.5.0
66
+ def compression_allowed?(command_name)
67
+ @compression_allowed ||= !REDACTED_COMMANDS.include?(command_name.to_s)
68
+ end
55
69
  end
56
70
  end
57
71
  end
@@ -17,6 +17,7 @@ require 'mongo/operation/executable'
17
17
  require 'mongo/operation/specifiable'
18
18
  require 'mongo/operation/limited'
19
19
  require 'mongo/operation/object_id_generator'
20
+ require 'mongo/operation/uses_command_op_msg'
20
21
  require 'mongo/operation/read_preference'
21
22
  require 'mongo/operation/takes_write_concern'
22
23
  require 'mongo/operation/read'
@@ -56,5 +57,20 @@ module Mongo
56
57
  #
57
58
  # @since 2.4.0
58
59
  COLLATION = 'collation'.freeze
60
+
61
+ # The array filters field constant.
62
+ #
63
+ # @since 2.5.0
64
+ ARRAY_FILTERS = 'arrayFilters'.freeze
65
+
66
+ # The operation time field constant.
67
+ #
68
+ # @since 2.5.0
69
+ OPERATION_TIME = 'operationTime'.freeze
70
+
71
+ # The cluster time field constant.
72
+ #
73
+ # @since 2.5.0
74
+ CLUSTER_TIME = '$clusterTime'.freeze
59
75
  end
60
76
  end
@@ -14,6 +14,7 @@
14
14
 
15
15
  require 'mongo/operation/commands/command'
16
16
  require 'mongo/operation/commands/find'
17
+ require 'mongo/operation/commands/explain'
17
18
  require 'mongo/operation/commands/get_more'
18
19
  require 'mongo/operation/commands/parallel_scan'
19
20
  require 'mongo/operation/commands/aggregate'
@@ -18,7 +18,7 @@ module Mongo
18
18
 
19
19
  # A MongoDB aggregate operation.
20
20
  #
21
- # @note An aggregate operation can behave like a read and return a
21
+ # @note An aggregate operation can behave like a read and return a
22
22
  # result set, or can behave like a write operation and
23
23
  # output results to a user-specified collection.
24
24
  #
@@ -50,11 +50,15 @@ module Mongo
50
50
  end
51
51
 
52
52
  def message(server)
53
- sel = update_selector_for_read_pref(selector, server)
54
- sel = filter_cursor_from_selector(sel, server)
53
+ sel = filter_cursor_from_selector(selector, server)
55
54
  sel = update_selector_for_write_concern(sel, server)
56
- opts = update_options_for_slave_ok(options, server)
57
- Protocol::Query.new(db_name, query_coll, sel, opts)
55
+ if server.features.op_msg_enabled?
56
+ command_op_msg(server, sel, options)
57
+ else
58
+ sel = update_selector_for_read_pref(sel, server)
59
+ opts = update_options_for_slave_ok(options, server)
60
+ Protocol::Query.new(db_name, query_coll, sel, opts)
61
+ end
58
62
  end
59
63
  end
60
64
  end
@@ -50,7 +50,7 @@ module Mongo
50
50
  #
51
51
  # @since 2.0.0
52
52
  def cursor_id
53
- cursor_document ? cursor_document[CURSOR_ID] : super
53
+ cursor_document ? cursor_document[CURSOR_ID] : 0
54
54
  end
55
55
 
56
56
  # Get the documents for the aggregation result. This is either the
@@ -18,16 +18,16 @@ module Mongo
18
18
  module Operation
19
19
  module Commands
20
20
 
21
- # A MongoDB operation to get a list of collection names in a database.
21
+ # A MongoDB operation to get a list of collections info in a database.
22
22
  #
23
- # @example Create the collection names operation.
24
- # Read::CollectionNames.new(:db_name => 'test-db')
23
+ # @example Create the collections info operation.
24
+ # CollectionsInfo.new(:db_name => 'test-db')
25
25
  #
26
26
  # Initialization:
27
- # param [ Hash ] spec The specifications for the collection names operation.
27
+ # param [ Hash ] spec The specifications for the collections info operation.
28
28
  #
29
- # option spec :db_name [ String ] The name of the database whose collection
30
- # names is requested.
29
+ # option spec :db_name [ String ] The name of the database whose collections
30
+ # info is requested.
31
31
  # option spec :options [ Hash ] Options for the operation.
32
32
  #
33
33
  # @since 2.0.0
@@ -19,7 +19,7 @@ module Mongo
19
19
  # A MongoDB command operation.
20
20
  #
21
21
  # @example Create the command operation.
22
- # Mongo::Operation::Command.new({ :selector => { :isMaster => 1 } })
22
+ # Command.new({ :selector => { :isMaster => 1 } })
23
23
  #
24
24
  # Initialization:
25
25
  # param [ Hash ] spec The specifications for the command.
@@ -35,6 +35,7 @@ module Mongo
35
35
  include Limited
36
36
  include ReadPreference
37
37
  include Executable
38
+ include UsesCommandOpMsg
38
39
 
39
40
  private
40
41
 
@@ -19,7 +19,7 @@ module Mongo
19
19
  # A MongoDB create collection operation.
20
20
  #
21
21
  # @example Instantiate the operation.
22
- # Commands::Create.new(selector: { create: 'test' }, :db_name => 'test')
22
+ # Create.new(selector: { create: 'test' }, :db_name => 'test')
23
23
  #
24
24
  # Initialization:
25
25
  # param [ Hash ] spec The specifications for the operation.
@@ -37,7 +37,11 @@ module Mongo
37
37
 
38
38
  def message(server)
39
39
  sel = update_selector_for_write_concern(selector, server)
40
- Protocol::Query.new(db_name, query_coll, sel, options)
40
+ if server.features.op_msg_enabled?
41
+ command_op_msg(server, sel, options)
42
+ else
43
+ Protocol::Query.new(db_name, query_coll, sel, options)
44
+ end
41
45
  end
42
46
  end
43
47
  end
@@ -19,7 +19,7 @@ module Mongo
19
19
  # A MongoDB drop collection operation.
20
20
  #
21
21
  # @example Instantiate the operation.
22
- # Commands::Drop.new(selector: { drop: 'test' }, :db_name => 'test')
22
+ # Drop.new(selector: { drop: 'test' }, :db_name => 'test')
23
23
  #
24
24
  # Initialization:
25
25
  # param [ Hash ] spec The specifications for the operation.
@@ -37,7 +37,11 @@ module Mongo
37
37
 
38
38
  def message(server)
39
39
  sel = update_selector_for_write_concern(selector, server)
40
- Protocol::Query.new(db_name, query_coll, sel, options)
40
+ if server.features.op_msg_enabled?
41
+ command_op_msg(server, sel, options)
42
+ else
43
+ Protocol::Query.new(db_name, query_coll, sel, options)
44
+ end
41
45
  end
42
46
  end
43
47
  end
@@ -19,7 +19,7 @@ module Mongo
19
19
  # A MongoDB drop database operation.
20
20
  #
21
21
  # @example Instantiate the operation.
22
- # Commands::Drop.new(selector: { dropDatabase: 'test' }, :db_name => 'test')
22
+ # Drop.new(selector: { dropDatabase: 'test' }, :db_name => 'test')
23
23
  #
24
24
  # Initialization:
25
25
  # param [ Hash ] spec The specifications for the operation.
@@ -37,7 +37,11 @@ module Mongo
37
37
 
38
38
  def message(server)
39
39
  sel = update_selector_for_write_concern(selector, server)
40
- Protocol::Query.new(db_name, query_coll, sel, options)
40
+ if server.features.op_msg_enabled?
41
+ command_op_msg(server, sel, options)
42
+ else
43
+ Protocol::Query.new(db_name, query_coll, sel, options)
44
+ end
41
45
  end
42
46
  end
43
47
  end
@@ -0,0 +1,27 @@
1
+ # Copyright (C) 2017 MongoDB, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module Mongo
16
+ module Operation
17
+ module Commands
18
+
19
+ # Encapsulates behaviour for executing a find command with explain.
20
+ #
21
+ # @since 2.5.0
22
+ class Explain < Command; end
23
+ end
24
+ end
25
+ end
26
+
27
+ require 'mongo/operation/commands/explain/result'
@@ -0,0 +1,52 @@
1
+ # Copyright (C) 2017 MongoDB, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module Mongo
16
+ module Operation
17
+ module Commands
18
+ class Explain
19
+
20
+ # Defines custom behaviour of results in find command with explain.
21
+ #
22
+ # @since 2.5.0
23
+ class Result < Operation::Result
24
+
25
+ # Get the cursor id.
26
+ #
27
+ # @example Get the cursor id.
28
+ # result.cursor_id
29
+ #
30
+ # @return [ 0 ] Always 0 because explain doesn't return a cursor.
31
+ #
32
+ # @since 2.5.0
33
+ def cursor_id
34
+ 0
35
+ end
36
+
37
+ # Get the documents in the result.
38
+ #
39
+ # @example Get the documents.
40
+ # result.documents
41
+ #
42
+ # @return [ Array<BSON::Document> ] The documents.
43
+ #
44
+ # @since 2.5.0
45
+ def documents
46
+ reply.documents
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -21,7 +21,7 @@ module Mongo
21
21
  # Initialize the get indexes operation.
22
22
  #
23
23
  # @example Instantiate the operation.
24
- # Read::Indexes.new(:db_name => 'test', :coll_name => 'test_coll')
24
+ # Indexes.new(:db_name => 'test', :coll_name => 'test_coll')
25
25
  #
26
26
  # Initialization:
27
27
  # param [ Hash ] spec The specifications for the insert.