mongo 2.0.6 → 2.1.0
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.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +5 -2
- data/lib/mongo/address/ipv4.rb +6 -1
- data/lib/mongo/address/unix.rb +2 -2
- data/lib/mongo/address.rb +18 -10
- data/lib/mongo/auth/cr/conversation.rb +1 -1
- data/lib/mongo/auth/ldap/conversation.rb +7 -3
- data/lib/mongo/auth/scram/conversation.rb +9 -3
- data/lib/mongo/auth/user/view.rb +23 -2
- data/lib/mongo/auth/x509/conversation.rb +1 -1
- data/lib/mongo/bulk_write/combineable.rb +51 -0
- data/lib/mongo/bulk_write/ordered_combiner.rb +55 -0
- data/lib/mongo/bulk_write/result.rb +191 -0
- data/lib/mongo/bulk_write/result_combiner.rb +117 -0
- data/lib/mongo/bulk_write/transformable.rb +132 -0
- data/lib/mongo/bulk_write/unordered_combiner.rb +52 -0
- data/lib/mongo/bulk_write/validatable.rb +62 -0
- data/lib/mongo/bulk_write.rb +159 -23
- data/lib/mongo/client.rb +52 -16
- data/lib/mongo/cluster/topology/replica_set.rb +27 -9
- data/lib/mongo/cluster/topology/sharded.rb +1 -1
- data/lib/mongo/cluster/topology/unknown.rb +5 -2
- data/lib/mongo/cluster.rb +42 -7
- data/lib/mongo/collection/view/aggregation.rb +48 -9
- data/lib/mongo/collection/view/immutable.rb +6 -6
- data/lib/mongo/collection/view/iterable.rb +18 -4
- data/lib/mongo/collection/view/map_reduce.rb +58 -17
- data/lib/mongo/collection/view/readable.rb +173 -42
- data/lib/mongo/collection/view/writable.rb +37 -23
- data/lib/mongo/collection/view.rb +2 -2
- data/lib/mongo/collection.rb +370 -33
- data/lib/mongo/cursor.rb +15 -3
- data/lib/mongo/database/view.rb +5 -4
- data/lib/mongo/database.rb +14 -4
- data/lib/mongo/dbref.rb +113 -0
- data/lib/mongo/error/closed_stream.rb +34 -0
- data/lib/mongo/error/extra_file_chunk.rb +34 -0
- data/lib/mongo/error/{invalid_uri_option.rb → file_not_found.rb} +11 -12
- data/lib/mongo/error/invalid_file.rb +2 -2
- data/lib/mongo/error/invalid_file_revision.rb +37 -0
- data/lib/mongo/error/invalid_uri.rb +5 -4
- data/lib/mongo/error/missing_file_chunk.rb +38 -0
- data/lib/mongo/error/operation_failure.rb +1 -1
- data/lib/mongo/error/parser.rb +1 -1
- data/lib/mongo/error/unchangeable_collection_option.rb +38 -0
- data/lib/mongo/error/unexpected_chunk_length.rb +39 -0
- data/lib/mongo/error.rb +13 -2
- data/lib/mongo/event/description_changed.rb +1 -1
- data/lib/mongo/grid/file/chunk.rb +6 -6
- data/lib/mongo/grid/file/{metadata.rb → info.rb} +41 -39
- data/lib/mongo/grid/file.rb +13 -10
- data/lib/mongo/grid/fs_bucket.rb +448 -0
- data/lib/mongo/grid/stream/read.rb +208 -0
- data/lib/mongo/grid/stream/write.rb +187 -0
- data/lib/mongo/grid/stream.rb +64 -0
- data/lib/mongo/grid.rb +2 -1
- data/lib/mongo/index/view.rb +3 -3
- data/lib/mongo/index.rb +5 -0
- data/lib/mongo/loggable.rb +34 -57
- data/lib/mongo/logger.rb +16 -78
- data/lib/mongo/monitoring/command_log_subscriber.rb +112 -0
- data/lib/mongo/monitoring/event/command_failed.rb +96 -0
- data/lib/mongo/monitoring/event/command_started.rb +89 -0
- data/lib/mongo/monitoring/event/command_succeeded.rb +118 -0
- data/lib/mongo/monitoring/event/secure.rb +58 -0
- data/lib/mongo/monitoring/event.rb +18 -0
- data/lib/mongo/monitoring/publishable.rb +106 -0
- data/lib/mongo/monitoring.rb +195 -0
- data/lib/mongo/operation/{aggregate.rb → commands/aggregate.rb} +3 -41
- data/lib/mongo/operation/commands/collections_info/result.rb +39 -0
- data/lib/mongo/operation/commands/collections_info.rb +68 -0
- data/lib/mongo/operation/{command.rb → commands/command.rb} +2 -18
- data/lib/mongo/operation/commands/indexes.rb +70 -0
- data/lib/mongo/operation/commands/list_collections/result.rb +112 -0
- data/lib/mongo/operation/commands/list_collections.rb +54 -0
- data/lib/mongo/operation/commands/list_indexes/result.rb +116 -0
- data/lib/mongo/operation/commands/list_indexes.rb +56 -0
- data/lib/mongo/operation/{map_reduce → commands/map_reduce}/result.rb +1 -1
- data/lib/mongo/operation/{map_reduce.rb → commands/map_reduce.rb} +3 -41
- data/lib/mongo/operation/commands/parallel_scan/result.rb +72 -0
- data/lib/mongo/operation/commands/parallel_scan.rb +56 -0
- data/lib/mongo/operation/commands/user_query.rb +69 -0
- data/lib/mongo/{bulk_write/ordered_bulk_write.rb → operation/commands/users_info/result.rb} +18 -30
- data/lib/mongo/operation/commands/users_info.rb +53 -0
- data/lib/mongo/operation/commands.rb +24 -0
- data/lib/mongo/operation/executable.rb +4 -68
- data/lib/mongo/operation/kill_cursors.rb +3 -3
- data/lib/mongo/operation/read/get_more.rb +2 -22
- data/lib/mongo/{bulk_write/unordered_bulk_write.rb → operation/read/query/result.rb} +20 -26
- data/lib/mongo/operation/read/query.rb +4 -21
- data/lib/mongo/operation/read.rb +0 -4
- data/lib/mongo/operation/{read_preferrable.rb → read_preference.rb} +3 -2
- data/lib/mongo/operation/result.rb +13 -1
- data/lib/mongo/operation/specifiable.rb +42 -0
- data/lib/mongo/operation/write/bulk/bulkable.rb +82 -0
- data/lib/mongo/operation/write/bulk/delete/result.rb +74 -0
- data/lib/mongo/operation/write/bulk/delete.rb +71 -0
- data/lib/mongo/operation/write/bulk/insert/result.rb +129 -0
- data/lib/mongo/operation/write/bulk/insert.rb +96 -0
- data/lib/mongo/operation/write/bulk/legacy_mergable.rb +87 -0
- data/lib/mongo/operation/write/bulk/mergable.rb +71 -0
- data/lib/mongo/operation/write/bulk/update/result.rb +174 -0
- data/lib/mongo/operation/write/bulk/update.rb +81 -0
- data/lib/mongo/operation/write/bulk.rb +6 -3
- data/lib/mongo/operation/write/command/create_index.rb +0 -1
- data/lib/mongo/operation/write/command/create_user.rb +0 -1
- data/lib/mongo/operation/write/command/delete.rb +0 -1
- data/lib/mongo/operation/write/command/drop_index.rb +0 -1
- data/lib/mongo/operation/write/command/insert.rb +0 -1
- data/lib/mongo/operation/write/command/remove_user.rb +0 -1
- data/lib/mongo/operation/write/command/update.rb +0 -1
- data/lib/mongo/operation/write/command/update_user.rb +0 -1
- data/lib/mongo/operation/write/command/writable.rb +13 -18
- data/lib/mongo/operation/write/create_index.rb +4 -27
- data/lib/mongo/operation/write/create_user.rb +4 -30
- data/lib/mongo/operation/write/delete.rb +6 -29
- data/lib/mongo/operation/write/drop_index.rb +3 -3
- data/lib/mongo/operation/write/gle.rb +48 -0
- data/lib/mongo/operation/write/idable.rb +5 -0
- data/lib/mongo/operation/write/insert.rb +2 -24
- data/lib/mongo/operation/write/remove_user.rb +4 -27
- data/lib/mongo/operation/write/update.rb +13 -36
- data/lib/mongo/operation/write/update_user.rb +4 -30
- data/lib/mongo/operation/write/write_command_enabled.rb +53 -0
- data/lib/mongo/operation/write.rb +2 -0
- data/lib/mongo/operation.rb +32 -4
- data/lib/mongo/options/mapper.rb +4 -2
- data/lib/mongo/options/redacted.rb +156 -0
- data/lib/mongo/options.rb +1 -0
- data/lib/mongo/protocol/delete.rb +75 -15
- data/lib/mongo/protocol/get_more.rb +65 -13
- data/lib/mongo/protocol/insert.rb +85 -13
- data/lib/mongo/protocol/kill_cursors.rb +59 -14
- data/lib/mongo/protocol/message.rb +12 -12
- data/lib/mongo/protocol/query.rb +163 -37
- data/lib/mongo/protocol/reply.rb +103 -0
- data/lib/mongo/protocol/serializers.rb +1 -1
- data/lib/mongo/protocol/update.rb +82 -14
- data/lib/mongo/retryable.rb +83 -0
- data/lib/mongo/server/connectable.rb +21 -25
- data/lib/mongo/server/connection.rb +75 -4
- data/lib/mongo/server/connection_pool/queue.rb +15 -0
- data/lib/mongo/server/connection_pool.rb +12 -0
- data/lib/mongo/server/description/features.rb +2 -1
- data/lib/mongo/server/description.rb +52 -1
- data/lib/mongo/server/monitor/connection.rb +26 -2
- data/lib/mongo/server/monitor.rb +19 -3
- data/lib/mongo/server.rb +39 -5
- data/lib/mongo/server_selector/selectable.rb +40 -31
- data/lib/mongo/server_selector.rb +19 -10
- data/lib/mongo/socket/ssl.rb +28 -16
- data/lib/mongo/socket/tcp.rb +3 -3
- data/lib/mongo/socket/unix.rb +5 -8
- data/lib/mongo/socket.rb +11 -4
- data/lib/mongo/uri.rb +248 -137
- data/lib/mongo/version.rb +1 -1
- data/lib/mongo.rb +5 -3
- data/spec/mongo/address/unix_spec.rb +1 -1
- data/spec/mongo/address_spec.rb +25 -0
- data/spec/mongo/auth/cr_spec.rb +9 -1
- data/spec/mongo/auth/ldap/conversation_spec.rb +43 -0
- data/spec/mongo/auth/ldap_spec.rb +9 -1
- data/spec/mongo/auth/scram_spec.rb +9 -1
- data/spec/mongo/auth/user/view_spec.rb +26 -1
- data/spec/mongo/auth/x509_spec.rb +9 -1
- data/spec/mongo/bulk_write/ordered_combiner_spec.rb +271 -0
- data/spec/mongo/bulk_write/unordered_combiner_spec.rb +239 -0
- data/spec/mongo/bulk_write_spec.rb +428 -0
- data/spec/mongo/client_spec.rb +167 -17
- data/spec/mongo/cluster/topology/replica_set_spec.rb +18 -9
- data/spec/mongo/cluster/topology/sharded_spec.rb +11 -3
- data/spec/mongo/cluster/topology/single_spec.rb +12 -4
- data/spec/mongo/cluster_spec.rb +55 -10
- data/spec/mongo/collection/view/aggregation_spec.rb +188 -1
- data/spec/mongo/collection/view/explainable_spec.rb +1 -1
- data/spec/mongo/collection/view/immutable_spec.rb +103 -0
- data/spec/mongo/collection/view/map_reduce_spec.rb +99 -4
- data/spec/mongo/collection/view/readable_spec.rb +238 -6
- data/spec/mongo/collection/view/writable_spec.rb +4 -4
- data/spec/mongo/collection/view_spec.rb +459 -71
- data/spec/mongo/collection_spec.rb +1291 -9
- data/spec/mongo/command_monitoring_spec.rb +51 -0
- data/spec/mongo/connection_string_spec.rb +115 -0
- data/spec/mongo/crud_spec.rb +2 -2
- data/spec/mongo/cursor_spec.rb +3 -3
- data/spec/mongo/database_spec.rb +47 -11
- data/spec/mongo/dbref_spec.rb +149 -0
- data/spec/mongo/grid/file/chunk_spec.rb +5 -5
- data/spec/mongo/grid/file/{metadata_spec.rb → info_spec.rb} +29 -17
- data/spec/mongo/grid/file_spec.rb +8 -8
- data/spec/mongo/grid/fs_bucket_spec.rb +1020 -0
- data/spec/mongo/grid/stream/read_spec.rb +275 -0
- data/spec/mongo/grid/stream/write_spec.rb +440 -0
- data/spec/mongo/grid/stream_spec.rb +48 -0
- data/spec/mongo/gridfs_spec.rb +50 -0
- data/spec/mongo/logger_spec.rb +0 -40
- data/spec/mongo/monitoring/command_log_subscriber_spec.rb +76 -0
- data/spec/mongo/monitoring/event/command_started_spec.rb +26 -0
- data/spec/mongo/monitoring/event/command_succeeded_spec.rb +26 -0
- data/spec/mongo/monitoring/event/secure_spec.rb +57 -0
- data/spec/mongo/monitoring_spec.rb +168 -0
- data/spec/mongo/operation/commands/aggregate_spec.rb +69 -0
- data/spec/mongo/operation/{read → commands}/collections_info_spec.rb +1 -1
- data/spec/mongo/operation/{command_spec.rb → commands/command_spec.rb} +0 -18
- data/spec/mongo/operation/{read → commands}/indexes_spec.rb +1 -1
- data/spec/mongo/operation/{map_reduce_spec.rb → commands/map_reduce_spec.rb} +1 -19
- data/spec/mongo/operation/kill_cursors_spec.rb +1 -17
- data/spec/mongo/operation/read/get_more_spec.rb +0 -16
- data/spec/mongo/operation/read/query_spec.rb +19 -16
- data/spec/mongo/operation/{read_preferrable_spec.rb → read_preference_spec.rb} +11 -11
- data/spec/mongo/operation/write/bulk/{bulk_delete_spec.rb → delete_spec.rb} +18 -29
- data/spec/mongo/operation/write/bulk/{bulk_insert_spec.rb → insert_spec.rb} +3 -14
- data/spec/mongo/operation/write/bulk/{bulk_update_spec.rb → update_spec.rb} +8 -19
- data/spec/mongo/operation/write/command/delete_spec.rb +0 -16
- data/spec/mongo/operation/write/command/insert_spec.rb +0 -16
- data/spec/mongo/operation/write/command/update_spec.rb +0 -16
- data/spec/mongo/operation/write/delete_spec.rb +4 -4
- data/spec/mongo/operation/write/insert_spec.rb +2 -13
- data/spec/mongo/operation/write/update_spec.rb +7 -7
- data/spec/mongo/options/redacted_spec.rb +350 -0
- data/spec/mongo/protocol/kill_cursors_spec.rb +5 -3
- data/spec/mongo/protocol/query_spec.rb +15 -30
- data/spec/mongo/retryable_spec.rb +147 -0
- data/spec/mongo/server/connection_pool/queue_spec.rb +16 -0
- data/spec/mongo/server/connection_pool_spec.rb +50 -6
- data/spec/mongo/server/connection_spec.rb +49 -4
- data/spec/mongo/server/description_spec.rb +49 -3
- data/spec/mongo/server/monitor_spec.rb +51 -0
- data/spec/mongo/server_discovery_and_monitoring_spec.rb +32 -59
- data/spec/mongo/server_selection_rtt_spec.rb +37 -57
- data/spec/mongo/server_selection_spec.rb +19 -9
- data/spec/mongo/server_selector/nearest_spec.rb +35 -27
- data/spec/mongo/server_selector/primary_preferred_spec.rb +32 -30
- data/spec/mongo/server_selector/primary_spec.rb +21 -14
- data/spec/mongo/server_selector/secondary_preferred_spec.rb +28 -26
- data/spec/mongo/server_selector/secondary_spec.rb +24 -22
- data/spec/mongo/server_selector_spec.rb +87 -24
- data/spec/mongo/server_spec.rb +94 -8
- data/spec/mongo/socket/ssl_spec.rb +123 -13
- data/spec/mongo/socket/unix_spec.rb +52 -0
- data/spec/mongo/uri_spec.rb +295 -67
- data/spec/spec_helper.rb +40 -24
- data/spec/support/authorization.rb +23 -9
- data/spec/support/certificates/client.pem +4 -4
- data/spec/support/command_monitoring/bulkWrite.yml +73 -0
- data/spec/support/command_monitoring/command.yml +42 -0
- data/spec/support/command_monitoring/deleteMany.yml +55 -0
- data/spec/support/command_monitoring/deleteOne.yml +55 -0
- data/spec/support/command_monitoring/find.yml +219 -0
- data/spec/support/command_monitoring/insertMany.yml +81 -0
- data/spec/support/command_monitoring/insertOne.yml +51 -0
- data/spec/support/command_monitoring/updateMany.yml +67 -0
- data/spec/support/command_monitoring/updateOne.yml +95 -0
- data/spec/support/command_monitoring.rb +365 -0
- data/spec/support/connection_string.rb +228 -0
- data/spec/support/connection_string_tests/invalid-uris.yml +193 -0
- data/spec/support/connection_string_tests/valid-auth.yml +256 -0
- data/spec/support/connection_string_tests/valid-host_identifiers.yml +121 -0
- data/spec/support/connection_string_tests/valid-options.yml +30 -0
- data/spec/support/connection_string_tests/valid-unix_socket-absolute.yml +197 -0
- data/spec/support/connection_string_tests/valid-unix_socket-relative.yml +213 -0
- data/spec/support/connection_string_tests/valid-warnings.yml +55 -0
- data/spec/support/crud/read.rb +22 -19
- data/spec/support/crud/write.rb +58 -27
- data/spec/support/crud.rb +10 -2
- data/spec/support/gridfs.rb +637 -0
- data/spec/support/gridfs_tests/delete.yml +157 -0
- data/spec/support/gridfs_tests/download.yml +210 -0
- data/spec/support/gridfs_tests/download_by_name.yml +113 -0
- data/spec/support/gridfs_tests/upload.yml +158 -0
- data/spec/support/matchers.rb +1 -1
- data/spec/support/sdam/rs/equal_electionids.yml +44 -0
- data/spec/support/sdam/rs/new_primary_new_electionid.yml +95 -0
- data/spec/support/sdam/rs/null_election_id.yml +144 -0
- data/spec/support/sdam/rs/primary_disconnect_electionid.yml +124 -0
- data/spec/support/sdam/rs/primary_mismatched_me.yml +37 -0
- data/spec/support/sdam/rs/primary_to_no_primary_mismatched_me.yml +75 -0
- data/spec/support/sdam/rs/secondary_mismatched_me.yml +37 -0
- data/spec/support/sdam/sharded/mongos_disconnect.yml +104 -0
- data/spec/support/sdam/single/direct_connection_rsarbiter.yml +1 -1
- data/spec/support/sdam/single/direct_connection_rsprimary.yml +1 -1
- data/spec/support/sdam/single/direct_connection_rssecondary.yml +1 -1
- data/spec/support/sdam/single/direct_connection_slave.yml +1 -1
- data/spec/support/sdam/single/direct_connection_standalone.yml +1 -1
- data/spec/support/sdam/single/not_ok_response.yml +0 -1
- data/spec/support/server_discovery_and_monitoring.rb +22 -3
- data/spec/support/server_selection.rb +3 -1
- data/spec/support/shared/bulk_write.rb +218 -22
- data/spec/support/shared/server_selector.rb +80 -14
- data.tar.gz.sig +0 -0
- metadata +188 -59
- metadata.gz.sig +0 -0
- data/lib/mongo/bulk_write/bulk_writable.rb +0 -196
- data/lib/mongo/bulk_write/deletable.rb +0 -56
- data/lib/mongo/bulk_write/insertable.rb +0 -48
- data/lib/mongo/bulk_write/replacable.rb +0 -57
- data/lib/mongo/bulk_write/updatable.rb +0 -68
- data/lib/mongo/grid/fs.rb +0 -149
- data/lib/mongo/operation/list_collections/result.rb +0 -114
- data/lib/mongo/operation/list_indexes/result.rb +0 -118
- data/lib/mongo/operation/read/collections_info.rb +0 -68
- data/lib/mongo/operation/read/indexes.rb +0 -69
- data/lib/mongo/operation/read/list_collections.rb +0 -76
- data/lib/mongo/operation/read/list_indexes.rb +0 -78
- data/lib/mongo/operation/write/bulk/bulk_delete/result.rb +0 -75
- data/lib/mongo/operation/write/bulk/bulk_delete.rb +0 -145
- data/lib/mongo/operation/write/bulk/bulk_insert/result.rb +0 -130
- data/lib/mongo/operation/write/bulk/bulk_insert.rb +0 -132
- data/lib/mongo/operation/write/bulk/bulk_mergable.rb +0 -67
- data/lib/mongo/operation/write/bulk/bulk_update/result.rb +0 -162
- data/lib/mongo/operation/write/bulk/bulk_update.rb +0 -154
- data/lib/mongo/operation/write/bulk/legacy_bulk_mergable.rb +0 -83
- data/spec/mongo/bulk/bulk_write_spec.rb +0 -262
- data/spec/mongo/grid/fs_spec.rb +0 -160
- data/spec/mongo/loggable_spec.rb +0 -63
- data/spec/mongo/operation/aggregate_spec.rb +0 -127
- /data/lib/mongo/operation/{aggregate → commands/aggregate}/result.rb +0 -0
@@ -34,10 +34,13 @@ module Mongo
|
|
34
34
|
cmd = { :findandmodify => collection.name, :query => selector, :remove => true }
|
35
35
|
cmd[:fields] = projection if projection
|
36
36
|
cmd[:sort] = sort if sort
|
37
|
-
|
37
|
+
cmd[:maxTimeMS] = max_time_ms if max_time_ms
|
38
|
+
write_with_retry do
|
39
|
+
database.command(cmd).first['value']
|
40
|
+
end
|
38
41
|
end
|
39
42
|
|
40
|
-
# Finds a single document and
|
43
|
+
# Finds a single document and replaces it.
|
41
44
|
#
|
42
45
|
# @example Find a document and replace it, returning the original.
|
43
46
|
# view.find_one_and_replace({ name: 'test' }, :return_document => :before)
|
@@ -45,7 +48,7 @@ module Mongo
|
|
45
48
|
# @example Find a document and replace it, returning the new document.
|
46
49
|
# view.find_one_and_replace({ name: 'test' }, :return_document => :after)
|
47
50
|
#
|
48
|
-
# @param [ BSON::Document ] replacement The
|
51
|
+
# @param [ BSON::Document ] replacement The replacement.
|
49
52
|
# @param [ Hash ] opts The options.
|
50
53
|
#
|
51
54
|
# @option opts [ Symbol ] :return_document Either :before or :after.
|
@@ -79,8 +82,11 @@ module Mongo
|
|
79
82
|
cmd[:sort] = sort if sort
|
80
83
|
cmd[:new] = !!(opts[:return_document] && opts[:return_document] == :after)
|
81
84
|
cmd[:upsert] = opts[:upsert] if opts[:upsert]
|
82
|
-
|
83
|
-
|
85
|
+
cmd[:maxTimeMS] = max_time_ms if max_time_ms
|
86
|
+
write_with_retry do
|
87
|
+
value = database.command(cmd).first['value']
|
88
|
+
value unless value.nil? || value.empty?
|
89
|
+
end
|
84
90
|
end
|
85
91
|
|
86
92
|
# Remove documents from the collection.
|
@@ -112,7 +118,7 @@ module Mongo
|
|
112
118
|
# @example Replace a single document.
|
113
119
|
# collection_view.replace_one({ name: 'test' })
|
114
120
|
#
|
115
|
-
# @param [ Hash ]
|
121
|
+
# @param [ Hash ] replacement The replacement document.
|
116
122
|
# @param [ Hash ] opts The options.
|
117
123
|
#
|
118
124
|
# @option opts [ true, false ] :upsert Whether to upsert if the
|
@@ -121,14 +127,16 @@ module Mongo
|
|
121
127
|
# @return [ Result ] The response from the database.
|
122
128
|
#
|
123
129
|
# @since 2.0.0
|
124
|
-
def replace_one(
|
125
|
-
update(
|
130
|
+
def replace_one(replacement, opts = {})
|
131
|
+
update(replacement, false, opts)
|
126
132
|
end
|
127
133
|
|
128
134
|
# Update documents in the collection.
|
129
135
|
#
|
130
136
|
# @example Update multiple documents in the collection.
|
131
137
|
# collection_view.update_many('$set' => { name: 'test' })
|
138
|
+
#
|
139
|
+
# @param [ Hash ] spec The update statement.
|
132
140
|
# @param [ Hash ] opts The options.
|
133
141
|
#
|
134
142
|
# @option opts [ true, false ] :upsert Whether to upsert if the
|
@@ -145,6 +153,8 @@ module Mongo
|
|
145
153
|
#
|
146
154
|
# @example Update a single document in the collection.
|
147
155
|
# collection_view.update_one('$set' => { name: 'test' })
|
156
|
+
#
|
157
|
+
# @param [ Hash ] spec The update statement.
|
148
158
|
# @param [ Hash ] opts The options.
|
149
159
|
#
|
150
160
|
# @option opts [ true, false ] :upsert Whether to upsert if the
|
@@ -160,24 +170,28 @@ module Mongo
|
|
160
170
|
private
|
161
171
|
|
162
172
|
def remove(value)
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
173
|
+
write_with_retry do
|
174
|
+
Operation::Write::Delete.new(
|
175
|
+
:delete => { Operation::Q => selector, Operation::LIMIT => value },
|
176
|
+
:db_name => collection.database.name,
|
177
|
+
:coll_name => collection.name,
|
178
|
+
:write_concern => collection.write_concern
|
179
|
+
).execute(next_primary.context)
|
180
|
+
end
|
169
181
|
end
|
170
182
|
|
171
183
|
def update(spec, multi, opts)
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
184
|
+
write_with_retry do
|
185
|
+
Operation::Write::Update.new(
|
186
|
+
:update => { Operation::Q => selector,
|
187
|
+
Operation::U => spec,
|
188
|
+
Operation::MULTI => multi,
|
189
|
+
Operation::UPSERT => !!opts[:upsert] },
|
190
|
+
:db_name => collection.database.name,
|
191
|
+
:coll_name => collection.name,
|
192
|
+
:write_concern => collection.write_concern
|
193
|
+
).execute(next_primary.context)
|
194
|
+
end
|
181
195
|
end
|
182
196
|
end
|
183
197
|
end
|
@@ -44,6 +44,7 @@ module Mongo
|
|
44
44
|
include Immutable
|
45
45
|
include Iterable
|
46
46
|
include Readable
|
47
|
+
include Retryable
|
47
48
|
include Explainable
|
48
49
|
include Writable
|
49
50
|
|
@@ -127,8 +128,7 @@ module Mongo
|
|
127
128
|
def initialize(collection, selector = {}, options = {})
|
128
129
|
validate_doc!(selector)
|
129
130
|
@collection = collection
|
130
|
-
|
131
|
-
@options = options.dup
|
131
|
+
setup(selector, options)
|
132
132
|
end
|
133
133
|
|
134
134
|
# Get a human-readable string representation of +View+.
|
data/lib/mongo/collection.rb
CHANGED
@@ -12,6 +12,7 @@
|
|
12
12
|
# See the License for the specific language governing permissions and
|
13
13
|
# limitations under the License.
|
14
14
|
|
15
|
+
require 'mongo/bulk_write'
|
15
16
|
require 'mongo/collection/view'
|
16
17
|
|
17
18
|
module Mongo
|
@@ -22,6 +23,17 @@ module Mongo
|
|
22
23
|
# @since 2.0.0
|
23
24
|
class Collection
|
24
25
|
extend Forwardable
|
26
|
+
include Retryable
|
27
|
+
|
28
|
+
# The capped option.
|
29
|
+
#
|
30
|
+
# @since 2.1.0
|
31
|
+
CAPPED = 'capped'.freeze
|
32
|
+
|
33
|
+
# The ns field constant.
|
34
|
+
#
|
35
|
+
# @since 2.1.0
|
36
|
+
NS = 'ns'.freeze
|
25
37
|
|
26
38
|
# @return [ Mongo::Database ] The database the collection resides in.
|
27
39
|
attr_reader :database
|
@@ -33,7 +45,7 @@ module Mongo
|
|
33
45
|
attr_reader :options
|
34
46
|
|
35
47
|
# Get client, cluster, read preference, and write concern from client.
|
36
|
-
def_delegators :database, :client, :cluster
|
48
|
+
def_delegators :database, :client, :cluster
|
37
49
|
|
38
50
|
# Delegate to the cluster for the next primary.
|
39
51
|
def_delegators :cluster, :next_primary
|
@@ -41,6 +53,11 @@ module Mongo
|
|
41
53
|
# Convenience delegators to find.
|
42
54
|
def_delegators :find, :parallel_scan
|
43
55
|
|
56
|
+
# Options that can be updated on a new Collection instance via the #with method.
|
57
|
+
#
|
58
|
+
# @since 2.1.0
|
59
|
+
CHANGEABLE_OPTIONS = [ :read, :write ]
|
60
|
+
|
44
61
|
# Check if a collection is equal to another object. Will check the name and
|
45
62
|
# the database for equality.
|
46
63
|
#
|
@@ -57,6 +74,70 @@ module Mongo
|
|
57
74
|
name == other.name && database == other.database && options == other.options
|
58
75
|
end
|
59
76
|
|
77
|
+
# Instantiate a new collection.
|
78
|
+
#
|
79
|
+
# @example Instantiate a new collection.
|
80
|
+
# Mongo::Collection.new(database, 'test')
|
81
|
+
#
|
82
|
+
# @param [ Mongo::Database ] database The collection's database.
|
83
|
+
# @param [ String, Symbol ] name The collection name.
|
84
|
+
# @param [ Hash ] options The collection options.
|
85
|
+
#
|
86
|
+
# @since 2.0.0
|
87
|
+
def initialize(database, name, options = {})
|
88
|
+
raise Error::InvalidCollectionName.new unless name
|
89
|
+
@database = database
|
90
|
+
@name = name.to_s.freeze
|
91
|
+
@options = options.freeze
|
92
|
+
end
|
93
|
+
|
94
|
+
# Get the read preference on this collection.
|
95
|
+
#
|
96
|
+
# @example Get the read preference.
|
97
|
+
# collection.read_preference
|
98
|
+
#
|
99
|
+
# @return [ Mongo::ServerSelector ] The read preference.
|
100
|
+
#
|
101
|
+
# @since 2.0.0
|
102
|
+
def read_preference
|
103
|
+
@read_preference ||= options[:read] ? ServerSelector.get(client.options.merge(options[:read])) :
|
104
|
+
database.read_preference
|
105
|
+
end
|
106
|
+
|
107
|
+
# Get the write concern on this collection.
|
108
|
+
#
|
109
|
+
# @example Get the write concern.
|
110
|
+
# collection.write_concern
|
111
|
+
#
|
112
|
+
# @return [ Mongo::WriteConcern ] The write concern.
|
113
|
+
#
|
114
|
+
# @since 2.0.0
|
115
|
+
def write_concern
|
116
|
+
@write_concern ||= options[:write] ? WriteConcern.get(options[:write]) :
|
117
|
+
database.write_concern
|
118
|
+
end
|
119
|
+
|
120
|
+
# Provides a new collection with either a new read preference or new write concern
|
121
|
+
# merged over the existing read preference / write concern.
|
122
|
+
#
|
123
|
+
# @example Get a collection with changed read preference.
|
124
|
+
# collection.with(:read => { :mode => :primary_preferred })
|
125
|
+
#
|
126
|
+
# @example Get a collection with changed write concern.
|
127
|
+
# collection.with(:write => { w: 3 })
|
128
|
+
|
129
|
+
# @param [ Hash ] new_options The new options to use.
|
130
|
+
#
|
131
|
+
# @return [ Mongo::Collection ] A new collection instance.
|
132
|
+
#
|
133
|
+
# @since 2.1.0
|
134
|
+
def with(new_options)
|
135
|
+
new_options.keys.each do |k|
|
136
|
+
raise Error::UnchangeableCollectionOption.new(k) unless CHANGEABLE_OPTIONS.include?(k)
|
137
|
+
end
|
138
|
+
Collection.new(database, name, options.merge(new_options))
|
139
|
+
end
|
140
|
+
|
60
141
|
# Is the collection capped?
|
61
142
|
#
|
62
143
|
# @example Is the collection capped?
|
@@ -66,7 +147,7 @@ module Mongo
|
|
66
147
|
#
|
67
148
|
# @since 2.0.0
|
68
149
|
def capped?
|
69
|
-
database.command(:collstats => name).documents[0][
|
150
|
+
database.command(:collstats => name).documents[0][CAPPED]
|
70
151
|
end
|
71
152
|
|
72
153
|
# Force the collection to be created in the database.
|
@@ -108,12 +189,96 @@ module Mongo
|
|
108
189
|
# collection.find
|
109
190
|
#
|
110
191
|
# @param [ Hash ] filter The filter to use in the find.
|
192
|
+
# @param [ Hash ] options The options for the find.
|
193
|
+
#
|
194
|
+
# @option options [ true, false ] :allow_partial_results Allows the query to get partial
|
195
|
+
# results if some shards are down.
|
196
|
+
# @option options [ Integer ] :batch_size The number of documents returned in each batch
|
197
|
+
# of results from MongoDB.
|
198
|
+
# @option options [ String ] :comment Associate a comment with the query.
|
199
|
+
# @option options [ :tailable, :tailable_await ] :cursor_type The type of cursor to use.
|
200
|
+
# @option options [ Integer ] :limit The max number of docs to return from the query.
|
201
|
+
# @option options [ Integer ] :max_time_ms The maximum amount of time to allow the query
|
202
|
+
# to run in milliseconds.
|
203
|
+
# @option options [ Hash ] :modifiers A document containing meta-operators modifying the
|
204
|
+
# output or behavior of a query.
|
205
|
+
# @option options [ true, false ] :no_cursor_timeout The server normally times out idle
|
206
|
+
# cursors after an inactivity period (10 minutes) to prevent excess memory use.
|
207
|
+
# Set this option to prevent that.
|
208
|
+
# @option options [ true, false ] :oplog_replay Internal replication use only - driver
|
209
|
+
# should not set.
|
210
|
+
# @option options [ Hash ] :projection The fields to include or exclude from each doc
|
211
|
+
# in the result set.
|
212
|
+
# @option options [ Integer ] :skip The number of docs to skip before returning results.
|
213
|
+
# @option options [ Hash ] :sort The key and direction pairs by which the result set
|
214
|
+
# will be sorted.
|
111
215
|
#
|
112
216
|
# @return [ CollectionView ] The collection view.
|
113
217
|
#
|
114
218
|
# @since 2.0.0
|
115
|
-
def find(filter = nil)
|
116
|
-
View.new(self, filter || {})
|
219
|
+
def find(filter = nil, options = {})
|
220
|
+
View.new(self, filter || {}, options)
|
221
|
+
end
|
222
|
+
|
223
|
+
# Perform an aggregation on the collection.
|
224
|
+
#
|
225
|
+
# @example Perform an aggregation.
|
226
|
+
# collection.aggregate([ { "$group" => { "_id" => "$city", "tpop" => { "$sum" => "$pop" }}} ])
|
227
|
+
#
|
228
|
+
# @param [ Array<Hash> ] pipeline The aggregation pipeline.
|
229
|
+
# @param [ Hash ] options The aggregation options.
|
230
|
+
#
|
231
|
+
# @option options [ true, false ] :allow_disk_use Set to true if disk usage is allowed during
|
232
|
+
# the aggregation.
|
233
|
+
# @option options [ Integer ] :batch_size The number of documents to return per batch.
|
234
|
+
# @option options [ Integer ] :max_time_ms The maximum amount of time in milliseconds to allow the
|
235
|
+
# aggregation to run.
|
236
|
+
# @option options [ true, false ] :use_cursor Indicates whether the command will request that the server
|
237
|
+
# provide results using a cursor.
|
238
|
+
#
|
239
|
+
# @return [ Aggregation ] The aggregation object.
|
240
|
+
#
|
241
|
+
# @since 2.1.0
|
242
|
+
def aggregate(pipeline, options = {})
|
243
|
+
View.new(self, {}).aggregate(pipeline, options)
|
244
|
+
end
|
245
|
+
|
246
|
+
# Get a count of matching documents in the collection.
|
247
|
+
#
|
248
|
+
# @example Get the count.
|
249
|
+
# collection.count(name: 1)
|
250
|
+
#
|
251
|
+
# @param [ Hash ] filter A filter for matching documents.
|
252
|
+
# @param [ Hash ] options The count options.
|
253
|
+
#
|
254
|
+
# @option options [ Hash ] :hint The index to use.
|
255
|
+
# @option options [ Integer ] :limit The maximum number of documents to count.
|
256
|
+
# @option options [ Integer ] :max_time_ms The maximum amount of time to allow the command to run.
|
257
|
+
# @option options [ Integer ] :skip The number of documents to skip before counting.
|
258
|
+
#
|
259
|
+
# @return [ Integer ] The document count.
|
260
|
+
#
|
261
|
+
# @since 2.1.0
|
262
|
+
def count(filter = nil, options = {})
|
263
|
+
View.new(self, filter || {}).count(options)
|
264
|
+
end
|
265
|
+
|
266
|
+
# Get a list of distinct values for a specific field.
|
267
|
+
#
|
268
|
+
# @example Get the distinct values.
|
269
|
+
# collection.distinct('name')
|
270
|
+
#
|
271
|
+
# @param [ Symbol, String ] field_name The name of the field.
|
272
|
+
# @param [ Hash ] filter The documents from which to retrieve the distinct values.
|
273
|
+
# @param [ Hash ] options The distinct command options.
|
274
|
+
#
|
275
|
+
# @option options [ Integer ] :max_time_ms The maximum amount of time to allow the command to run.
|
276
|
+
#
|
277
|
+
# @return [ Array<Object> ] The list of distinct values.
|
278
|
+
#
|
279
|
+
# @since 2.1.0
|
280
|
+
def distinct(field_name, filter = nil, options = {})
|
281
|
+
View.new(self, filter || {}).distinct(field_name, options)
|
117
282
|
end
|
118
283
|
|
119
284
|
# Get a view of all indexes for this collection. Can be iterated or has
|
@@ -131,23 +296,6 @@ module Mongo
|
|
131
296
|
Index::View.new(self, options)
|
132
297
|
end
|
133
298
|
|
134
|
-
# Instantiate a new collection.
|
135
|
-
#
|
136
|
-
# @example Instantiate a new collection.
|
137
|
-
# Mongo::Collection.new(database, 'test')
|
138
|
-
#
|
139
|
-
# @param [ Mongo::Database ] database The collection's database.
|
140
|
-
# @param [ String, Symbol ] name The collection name.
|
141
|
-
# @param [ Hash ] options The collection options.
|
142
|
-
#
|
143
|
-
# @since 2.0.0
|
144
|
-
def initialize(database, name, options = {})
|
145
|
-
raise Error::InvalidCollectionName.new unless name
|
146
|
-
@database = database
|
147
|
-
@name = name.to_s.freeze
|
148
|
-
@options = options.freeze
|
149
|
-
end
|
150
|
-
|
151
299
|
# Get a pretty printed string inspection for the collection.
|
152
300
|
#
|
153
301
|
# @example Inspect the collection.
|
@@ -172,7 +320,15 @@ module Mongo
|
|
172
320
|
#
|
173
321
|
# @since 2.0.0
|
174
322
|
def insert_one(document, options = {})
|
175
|
-
|
323
|
+
write_with_retry do
|
324
|
+
Operation::Write::Insert.new(
|
325
|
+
:documents => [ document ],
|
326
|
+
:db_name => database.name,
|
327
|
+
:coll_name => name,
|
328
|
+
:write_concern => write_concern,
|
329
|
+
:options => options
|
330
|
+
).execute(next_primary.context)
|
331
|
+
end
|
176
332
|
end
|
177
333
|
|
178
334
|
# Insert the provided documents into the collection.
|
@@ -187,13 +343,8 @@ module Mongo
|
|
187
343
|
#
|
188
344
|
# @since 2.0.0
|
189
345
|
def insert_many(documents, options = {})
|
190
|
-
|
191
|
-
|
192
|
-
:db_name => database.name,
|
193
|
-
:coll_name => name,
|
194
|
-
:write_concern => write_concern,
|
195
|
-
:options => options
|
196
|
-
).execute(next_primary.context)
|
346
|
+
inserts = documents.map{ |doc| { :insert_one => doc }}
|
347
|
+
bulk_write(inserts, options)
|
197
348
|
end
|
198
349
|
|
199
350
|
# Execute a batch of bulk write operations.
|
@@ -201,14 +352,200 @@ module Mongo
|
|
201
352
|
# @example Execute a bulk write.
|
202
353
|
# collection.bulk_write(operations, options)
|
203
354
|
#
|
204
|
-
# @param [ Array<Hash> ]
|
355
|
+
# @param [ Array<Hash> ] requests The bulk write requests.
|
205
356
|
# @param [ Hash ] options The options.
|
206
357
|
#
|
207
|
-
# @
|
358
|
+
# @option options [ true, false ] :ordered Whether the operations
|
359
|
+
# should be executed in order.
|
360
|
+
# @option options [ Hash ] :write_concern The write concern options.
|
361
|
+
# Can be :w => Integer, :fsync => Boolean, :j => Boolean.
|
362
|
+
#
|
363
|
+
# @return [ BulkWrite::Result ] The result of the operation.
|
208
364
|
#
|
209
365
|
# @since 2.0.0
|
210
|
-
def bulk_write(
|
211
|
-
BulkWrite.
|
366
|
+
def bulk_write(requests, options = {})
|
367
|
+
BulkWrite.new(self, requests, options).execute
|
368
|
+
end
|
369
|
+
|
370
|
+
# Remove a document from the collection.
|
371
|
+
#
|
372
|
+
# @example Remove a single document from the collection.
|
373
|
+
# collection.delete_one
|
374
|
+
#
|
375
|
+
# @param [ Hash ] filter The filter to use.
|
376
|
+
#
|
377
|
+
# @return [ Result ] The response from the database.
|
378
|
+
#
|
379
|
+
# @since 2.1.0
|
380
|
+
def delete_one(filter = nil)
|
381
|
+
find(filter).delete_one
|
382
|
+
end
|
383
|
+
|
384
|
+
# Remove documents from the collection.
|
385
|
+
#
|
386
|
+
# @example Remove multiple documents from the collection.
|
387
|
+
# collection.delete_many
|
388
|
+
#
|
389
|
+
# @param [ Hash ] filter The filter to use.
|
390
|
+
#
|
391
|
+
# @return [ Result ] The response from the database.
|
392
|
+
#
|
393
|
+
# @since 2.1.0
|
394
|
+
def delete_many(filter = nil)
|
395
|
+
find(filter).delete_many
|
396
|
+
end
|
397
|
+
|
398
|
+
# Execute a parallel scan on the collection view.
|
399
|
+
#
|
400
|
+
# Returns a list of up to cursor_count cursors that can be iterated concurrently.
|
401
|
+
# As long as the collection is not modified during scanning, each document appears once
|
402
|
+
# in one of the cursors' result sets.
|
403
|
+
#
|
404
|
+
# @example Execute a parallel collection scan.
|
405
|
+
# collection.parallel_scan(2)
|
406
|
+
#
|
407
|
+
# @param [ Integer ] cursor_count The max number of cursors to return.
|
408
|
+
#
|
409
|
+
# @return [ Array<Cursor> ] An array of cursors.
|
410
|
+
#
|
411
|
+
# @since 2.1
|
412
|
+
def parallel_scan(cursor_count)
|
413
|
+
find.send(:parallel_scan, cursor_count)
|
414
|
+
end
|
415
|
+
|
416
|
+
# Replaces a single document in the collection with the new document.
|
417
|
+
#
|
418
|
+
# @example Replace a single document.
|
419
|
+
# collection.replace_one({ name: 'test' }, { name: 'test1' })
|
420
|
+
#
|
421
|
+
# @param [ Hash ] filter The filter to use.
|
422
|
+
# @param [ Hash ] replacement The replacement document..
|
423
|
+
# @param [ Hash ] options The options.
|
424
|
+
#
|
425
|
+
# @option options [ true, false ] :upsert Whether to upsert if the
|
426
|
+
# document doesn't exist.
|
427
|
+
#
|
428
|
+
# @return [ Result ] The response from the database.
|
429
|
+
#
|
430
|
+
# @since 2.1.0
|
431
|
+
def replace_one(filter, replacement, options = {})
|
432
|
+
find(filter).replace_one(replacement, options)
|
433
|
+
end
|
434
|
+
|
435
|
+
# Update documents in the collection.
|
436
|
+
#
|
437
|
+
# @example Update multiple documents in the collection.
|
438
|
+
# collection.update_many({ name: 'test'}, '$set' => { name: 'test1' })
|
439
|
+
#
|
440
|
+
# @param [ Hash ] filter The filter to use.
|
441
|
+
# @param [ Hash ] update The update statement.
|
442
|
+
# @param [ Hash ] options The options.
|
443
|
+
#
|
444
|
+
# @option options [ true, false ] :upsert Whether to upsert if the
|
445
|
+
# document doesn't exist.
|
446
|
+
#
|
447
|
+
# @return [ Result ] The response from the database.
|
448
|
+
#
|
449
|
+
# @since 2.1.0
|
450
|
+
def update_many(filter, update, options = {})
|
451
|
+
find(filter).update_many(update, options)
|
452
|
+
end
|
453
|
+
|
454
|
+
# Update a single document in the collection.
|
455
|
+
#
|
456
|
+
# @example Update a single document in the collection.
|
457
|
+
# collection.update_one({ name: 'test'}, '$set' => { name: 'test1'})
|
458
|
+
#
|
459
|
+
# @param [ Hash ] filter The filter to use.
|
460
|
+
# @param [ Hash ] update The update statement.
|
461
|
+
# @param [ Hash ] options The options.
|
462
|
+
#
|
463
|
+
# @option options [ true, false ] :upsert Whether to upsert if the
|
464
|
+
# document doesn't exist.
|
465
|
+
#
|
466
|
+
# @return [ Result ] The response from the database.
|
467
|
+
#
|
468
|
+
# @since 2.1.0
|
469
|
+
def update_one(filter, update, options = {})
|
470
|
+
find(filter).update_one(update, options)
|
471
|
+
end
|
472
|
+
|
473
|
+
# Finds a single document in the database via findAndModify and deletes
|
474
|
+
# it, returning the original document.
|
475
|
+
#
|
476
|
+
# @example Find one document and delete it.
|
477
|
+
# collection.find_one_and_delete(name: 'test')
|
478
|
+
#
|
479
|
+
# @param [ Hash ] filter The filter to use.
|
480
|
+
# @param [ Hash ] options The options.
|
481
|
+
#
|
482
|
+
# @option options [ Integer ] :max_time_ms The maximum amount of time to allow the command
|
483
|
+
# to run in milliseconds.
|
484
|
+
# @option options [ Hash ] :projection The fields to include or exclude in the returned doc.
|
485
|
+
# @option options [ Hash ] :sort The key and direction pairs by which the result set
|
486
|
+
# will be sorted.
|
487
|
+
#
|
488
|
+
# @return [ BSON::Document, nil ] The document, if found.
|
489
|
+
#
|
490
|
+
# @since 2.1.0
|
491
|
+
def find_one_and_delete(filter, options = {})
|
492
|
+
find(filter, options).find_one_and_delete
|
493
|
+
end
|
494
|
+
|
495
|
+
# Finds a single document via findAndModify and updates it, returning the original doc unless
|
496
|
+
# otherwise specified.
|
497
|
+
#
|
498
|
+
# @example Find a document and update it, returning the original.
|
499
|
+
# collection.find_one_and_update({ name: 'test' }, { "$set" => { name: 'test1' }})
|
500
|
+
#
|
501
|
+
# @example Find a document and update it, returning the updated document.
|
502
|
+
# collection.find_one_and_update({ name: 'test' }, { "$set" => { name: 'test1' }}, :return_document => :after)
|
503
|
+
#
|
504
|
+
# @param [ Hash ] filter The filter to use.
|
505
|
+
# @param [ BSON::Document ] update The update statement.
|
506
|
+
# @param [ Hash ] options The options.
|
507
|
+
#
|
508
|
+
# @option options [ Integer ] :max_time_ms The maximum amount of time to allow the command
|
509
|
+
# to run in milliseconds.
|
510
|
+
# @option options [ Hash ] :projection The fields to include or exclude in the returned doc.
|
511
|
+
# @option options [ Hash ] :sort The key and direction pairs by which the result set
|
512
|
+
# will be sorted.
|
513
|
+
# @option options [ Symbol ] :return_document Either :before or :after.
|
514
|
+
# @option options [ true, false ] :upsert Whether to upsert if the document doesn't exist.
|
515
|
+
#
|
516
|
+
# @return [ BSON::Document ] The document.
|
517
|
+
#
|
518
|
+
# @since 2.1.0
|
519
|
+
def find_one_and_update(filter, update, options = {})
|
520
|
+
find(filter, options).find_one_and_update(update, options)
|
521
|
+
end
|
522
|
+
|
523
|
+
# Finds a single document and replaces it, returning the original doc unless
|
524
|
+
# otherwise specified.
|
525
|
+
#
|
526
|
+
# @example Find a document and replace it, returning the original.
|
527
|
+
# collection.find_one_and_replace({ name: 'test' }, { name: 'test1' })
|
528
|
+
#
|
529
|
+
# @example Find a document and replace it, returning the new document.
|
530
|
+
# collection.find_one_and_replace({ name: 'test' }, { name: 'test1' }, :return_document => :after)
|
531
|
+
#
|
532
|
+
# @param [ Hash ] filter The filter to use.
|
533
|
+
# @param [ BSON::Document ] replacement The replacement document.
|
534
|
+
# @param [ Hash ] options The options.
|
535
|
+
#
|
536
|
+
# @option options [ Integer ] :max_time_ms The maximum amount of time to allow the command
|
537
|
+
# to run in milliseconds.
|
538
|
+
# @option options [ Hash ] :projection The fields to include or exclude in the returned doc.
|
539
|
+
# @option options [ Hash ] :sort The key and direction pairs by which the result set
|
540
|
+
# will be sorted.
|
541
|
+
# @option options [ Symbol ] :return_document Either :before or :after.
|
542
|
+
# @option options [ true, false ] :upsert Whether to upsert if the document doesn't exist.
|
543
|
+
#
|
544
|
+
# @return [ BSON::Document ] The document.
|
545
|
+
#
|
546
|
+
# @since 2.1.0
|
547
|
+
def find_one_and_replace(filter, replacement, options = {})
|
548
|
+
find(filter, options).find_one_and_update(replacement, options)
|
212
549
|
end
|
213
550
|
|
214
551
|
# Get the fully qualified namespace of the collection.
|
data/lib/mongo/cursor.rb
CHANGED
@@ -97,12 +97,16 @@ module Mongo
|
|
97
97
|
end
|
98
98
|
|
99
99
|
def get_more_operation
|
100
|
-
Operation::Read::GetMore.new(
|
100
|
+
Operation::Read::GetMore.new(get_more_spec)
|
101
|
+
end
|
102
|
+
|
103
|
+
def get_more_spec
|
104
|
+
{
|
101
105
|
:to_return => to_return,
|
102
106
|
:cursor_id => @cursor_id,
|
103
107
|
:db_name => database.name,
|
104
108
|
:coll_name => @coll_name || collection.name
|
105
|
-
}
|
109
|
+
}
|
106
110
|
end
|
107
111
|
|
108
112
|
def kill_cursors
|
@@ -110,7 +114,15 @@ module Mongo
|
|
110
114
|
end
|
111
115
|
|
112
116
|
def kill_cursors_operation
|
113
|
-
Operation::KillCursors.new(
|
117
|
+
Operation::KillCursors.new(kill_cursors_spec)
|
118
|
+
end
|
119
|
+
|
120
|
+
def kill_cursors_spec
|
121
|
+
{
|
122
|
+
:coll_name => @coll_name || collection.name,
|
123
|
+
:db_name => database.name,
|
124
|
+
:cursor_ids => [ @cursor_id ]
|
125
|
+
}
|
114
126
|
end
|
115
127
|
|
116
128
|
def limited?
|
data/lib/mongo/database/view.rb
CHANGED
@@ -51,9 +51,10 @@ module Mongo
|
|
51
51
|
def collection_names(options = {})
|
52
52
|
@batch_size = options[:batch_size]
|
53
53
|
server = next_primary
|
54
|
+
@limit = -1 if server.features.list_collections_enabled?
|
54
55
|
collections_info(server).collect do |info|
|
55
|
-
server.
|
56
|
-
info[
|
56
|
+
server.features.list_collections_enabled? ?
|
57
|
+
info[Database::NAME] : info[Database::NAME].sub("#{@database.name}.", '')
|
57
58
|
end
|
58
59
|
end
|
59
60
|
|
@@ -80,7 +81,7 @@ module Mongo
|
|
80
81
|
def initialize(database)
|
81
82
|
@database = database
|
82
83
|
@batch_size = nil
|
83
|
-
@limit =
|
84
|
+
@limit = nil
|
84
85
|
@collection = @database[Database::COMMAND]
|
85
86
|
end
|
86
87
|
|
@@ -102,7 +103,7 @@ module Mongo
|
|
102
103
|
end
|
103
104
|
|
104
105
|
def initial_query_op
|
105
|
-
Operation::
|
106
|
+
Operation::CollectionsInfo.new(collections_info_spec)
|
106
107
|
end
|
107
108
|
|
108
109
|
def send_initial_query(server)
|