Skip to content

Commit f5fd06a

Browse files
committed
RUBY-1673 Document collection options (#2588)
* RUBY-1673 Document collection options * RUBY-1673 remove read_preference option * RUBY-1673 amend #with docs to include read_concern * RUBY-1673 add missing options * RUBY-1673 verify max and size options work * RUBY-1673 nitpick * RUBY-1673 alphabetize the options * RUBY-1673 fix mmap tests * RUBY-1673 fix mmap tests
1 parent 9e87516 commit f5fd06a

File tree

5 files changed

+162
-41
lines changed

5 files changed

+162
-41
lines changed

lib/mongo/collection.rb

Lines changed: 110 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -100,19 +100,59 @@ def ==(other)
100100
# @param [ String, Symbol ] name The collection name.
101101
# @param [ Hash ] options The collection options.
102102
#
103-
# @option options [ Hash ] :write Deprecated. Equivalent to :write_concern
103+
# @option opts [ true | false ] :capped Create a fixed-sized collection.
104+
# @option opts [ Hash ] :change_stream_pre_and_post_images Used to enable
105+
# pre- and post-images on the created collection.
106+
# The hash may have the following items:
107+
# - *:enabled* -- true or false.
108+
# @option opts [ Hash ] :clustered_index Create a clustered index.
109+
# This option specifies how this collection should be clustered on _id.
110+
# The hash may have the following items:
111+
# - *:key* -- The clustered index key field. Must be set to { _id: 1 }.
112+
# - *:unique* -- Must be set to true. The collection will not accept
113+
# inserted or updated documents where the clustered index key value
114+
# matches an existing value in the index.
115+
# - *:name* -- Optional. A name that uniquely identifies the clustered index.
116+
# @option opts [ Hash ] :collation The collation to use.
117+
# @option opts [ Hash ] :encrypted_fields Hash describing encrypted fields
118+
# for queryable encryption.
119+
# @option opts [ Integer ] :expire_after Number indicating
120+
# after how many seconds old time-series data should be deleted.
121+
# @option opts [ Integer ] :max The maximum number of documents in a
122+
# capped collection. The size limit takes precedents over max.
123+
# @option opts [ Array<Hash> ] :pipeline An array of pipeline stages.
124+
# A view will be created by applying this pipeline to the view_on
125+
# collection or view.
126+
# @option options [ Hash ] :read_concern The read concern options hash,
127+
# with the following optional keys:
128+
# - *:level* -- the read preference level as a symbol; valid values
129+
# are *:local*, *:majority*, and *:snapshot*
130+
# @option options [ Hash ] :read The read preference options.
131+
# The hash may have the following items:
132+
# - *:mode* -- read preference specified as a symbol; valid values are
133+
# *:primary*, *:primary_preferred*, *:secondary*, *:secondary_preferred*
134+
# and *:nearest*.
135+
# - *:tag_sets* -- an array of hashes.
136+
# - *:local_threshold*.
137+
# @option opts [ Session ] :session The session to use for the operation.
138+
# @option opts [ Integer ] :size The size of the capped collection.
139+
# @option opts [ Hash ] :time_series Create a time-series collection.
140+
# The hash may have the following items:
141+
# - *:timeField* -- The name of the field which contains the date in each
142+
# time series document.
143+
# - *:metaField* -- The name of the field which contains metadata in each
144+
# time series document.
145+
# - *:granularity* -- Set the granularity to the value that is the closest
146+
# match to the time span between consecutive incoming measurements.
147+
# Possible values are "seconds" (default), "minutes", and "hours".
148+
# @option opts [ Hash ] :validator Hash describing document validation
149+
# options for the collection.
150+
# @option opts [ String ] :view_on The name of the source collection or
151+
# view from which to create a view.
152+
# @option opts [ Hash ] :write Deprecated. Equivalent to :write_concern
104153
# option.
105-
# @option options [ Hash ] :write_concern The write concern options.
154+
# @option opts [ Hash ] :write_concern The write concern options.
106155
# Can be :w => Integer|String, :fsync => Boolean, :j => Boolean.
107-
# @option options [ Hash ] :time_series Create a time-series collection.
108-
# See https://mongodb.com/docs/manual/core/timeseries-collections/ for more
109-
# information about time-series collection.
110-
# @option options [ Integer ] :expire_after Number indicating
111-
# after how many seconds old time-series data should be deleted.
112-
# @options clustered_index [ Hash ] :clustered_index Create a clustered index.
113-
# This option specifies how this collection should be clustered on _id.
114-
# See https://www.mongodb.com/docs/v5.3/reference/method/db.createCollection/#std-label-db.createCollection.clusteredIndex
115-
# for more information about this option.
116156
#
117157
# @since 2.0.0
118158
def initialize(database, name, options = {})
@@ -204,17 +244,37 @@ def write_concern_with_session(session)
204244
wc
205245
end
206246

207-
# Provides a new collection with either a new read preference or new write concern
208-
# merged over the existing read preference / write concern.
247+
# Provides a new collection with either a new read preference, new read
248+
# concern or new write concern merged over the existing read preference /
249+
# read concern / write concern.
209250
#
210251
# @example Get a collection with a changed read preference.
211252
# collection.with(read: { mode: :primary_preferred })
253+
254+
# @example Get a collection with a changed read concern.
255+
# collection.with(read_concern: { level: :majority })
212256
#
213257
# @example Get a collection with a changed write concern.
214258
# collection.with(write_concern: { w: 3 })
215-
259+
#
216260
# @param [ Hash ] new_options The new options to use.
217261
#
262+
# @option new_options [ Hash ] :read The read preference options.
263+
# The hash may have the following items:
264+
# - *:mode* -- read preference specified as a symbol; valid values are
265+
# *:primary*, *:primary_preferred*, *:secondary*, *:secondary_preferred*
266+
# and *:nearest*.
267+
# - *:tag_sets* -- an array of hashes.
268+
# - *:local_threshold*.
269+
# @option new_options [ Hash ] :read_concern The read concern options hash,
270+
# with the following optional keys:
271+
# - *:level* -- the read preference level as a symbol; valid values
272+
# are *:local*, *:majority*, and *:snapshot*
273+
# @option new_options [ Hash ] :write Deprecated. Equivalent to :write_concern
274+
# option.
275+
# @option new_options [ Hash ] :write_concern The write concern options.
276+
# Can be :w => Integer|String, :fsync => Boolean, :j => Boolean.
277+
#
218278
# @return [ Mongo::Collection ] A new collection instance.
219279
#
220280
# @since 2.1.0
@@ -251,17 +311,48 @@ def capped?
251311
#
252312
# @param [ Hash ] opts The options for the create operation.
253313
#
254-
# @option opts [ Session ] :session The session to use for the operation.
255-
# @option opts [ Hash ] :write_concern The write concern options.
256-
# @option opts [ Hash ] :time_series Create a time-series collection.
257-
# @option opts [ Integer ] :expire_after Number indicating
258-
# after how many seconds old time-series data should be deleted.
314+
# @option opts [ true | false ] :capped Create a fixed-sized collection.
259315
# @option opts [ Hash ] :change_stream_pre_and_post_images Used to enable
260316
# pre- and post-images on the created collection.
317+
# The hash may have the following items:
318+
# - *:enabled* -- true or false.
319+
# @option opts [ Hash ] :clustered_index Create a clustered index.
320+
# This option specifies how this collection should be clustered on _id.
321+
# The hash may have the following items:
322+
# - *:key* -- The clustered index key field. Must be set to { _id: 1 }.
323+
# - *:unique* -- Must be set to true. The collection will not accept
324+
# inserted or updated documents where the clustered index key value
325+
# matches an existing value in the index.
326+
# - *:name* -- Optional. A name that uniquely identifies the clustered index.
327+
# @option opts [ Hash ] :collation The collation to use.
261328
# @option opts [ Hash ] :encrypted_fields Hash describing encrypted fields
262329
# for queryable encryption.
330+
# @option opts [ Integer ] :expire_after Number indicating
331+
# after how many seconds old time-series data should be deleted.
332+
# @option opts [ Integer ] :max The maximum number of documents in a
333+
# capped collection. The size limit takes precedents over max.
334+
# @option opts [ Array<Hash> ] :pipeline An array of pipeline stages.
335+
# A view will be created by applying this pipeline to the view_on
336+
# collection or view.
337+
# @option opts [ Session ] :session The session to use for the operation.
338+
# @option opts [ Integer ] :size The size of the capped collection.
339+
# @option opts [ Hash ] :time_series Create a time-series collection.
340+
# The hash may have the following items:
341+
# - *:timeField* -- The name of the field which contains the date in each
342+
# time series document.
343+
# - *:metaField* -- The name of the field which contains metadata in each
344+
# time series document.
345+
# - *:granularity* -- Set the granularity to the value that is the closest
346+
# match to the time span between consecutive incoming measurements.
347+
# Possible values are "seconds" (default), "minutes", and "hours".
263348
# @option opts [ Hash ] :validator Hash describing document validation
264349
# options for the collection.
350+
# @option opts [ String ] :view_on The name of the source collection or
351+
# view from which to create a view.
352+
# @option opts [ Hash ] :write Deprecated. Equivalent to :write_concern
353+
# option.
354+
# @option opts [ Hash ] :write_concern The write concern options.
355+
# Can be :w => Integer|String, :fsync => Boolean, :j => Boolean.
265356
#
266357
# @return [ Result ] The result of the command.
267358
#

lib/mongo/query_cache.rb

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -114,24 +114,24 @@ def clear_namespace(namespace)
114114
#
115115
# @param [ Mongo::CachingCursor ] cursor The CachingCursor instance to store.
116116
#
117-
# @option opts [ String | nil ] namespace The namespace of the query,
117+
# @option opts [ String | nil ] :namespace The namespace of the query,
118118
# in the format "database_name.collection_name".
119-
# @option opts [ Array, Hash ] selector The selector passed to the query.
119+
# @option opts [ Array, Hash ] :selector The selector passed to the query.
120120
# For most queries, this will be a Hash, but for aggregations, this
121121
# will be an Array representing the aggregation pipeline. May not be nil.
122-
# @option opts [ Integer | nil ] skip The skip value of the query.
123-
# @option opts [ Hash | nil ] sort The order of the query results
122+
# @option opts [ Integer | nil ] :skip The skip value of the query.
123+
# @option opts [ Hash | nil ] :sort The order of the query results
124124
# (e.g. { name: -1 }).
125-
# @option opts [ Integer | nil ] limit The limit value of the query.
126-
# @option opts [ Hash | nil ] projection The projection of the query
125+
# @option opts [ Integer | nil ] :limit The limit value of the query.
126+
# @option opts [ Hash | nil ] :projection The projection of the query
127127
# results (e.g. { name: 1 }).
128-
# @option opts [ Hash | nil ] collation The collation of the query
128+
# @option opts [ Hash | nil ] :collation The collation of the query
129129
# (e.g. { "locale" => "fr_CA" }).
130-
# @option opts [ Hash | nil ] read_concern The read concern of the query
130+
# @option opts [ Hash | nil ] :read_concern The read concern of the query
131131
# (e.g. { level: :majority }).
132-
# @option opts [ Hash | nil ] read_preference The read preference of
132+
# @option opts [ Hash | nil ] :read_preference The read preference of
133133
# the query (e.g. { mode: :secondary }).
134-
# @option opts [ Boolean | nil ] multi_collection Whether the query
134+
# @option opts [ Boolean | nil ] :multi_collection Whether the query
135135
# results could potentially come from multiple collections. When true,
136136
# these results will be stored under the nil namespace key and cleared
137137
# on every write command.
@@ -152,24 +152,24 @@ def set(cursor, **opts)
152152
# For the given query options, retrieve a cached cursor that can be used
153153
# to obtain the correct query results, if one exists in the cache.
154154
#
155-
# @option opts [ String | nil ] namespace The namespace of the query,
155+
# @option opts [ String | nil ] :namespace The namespace of the query,
156156
# in the format "database_name.collection_name".
157-
# @option opts [ Array, Hash ] selector The selector passed to the query.
157+
# @option opts [ Array, Hash ] :selector The selector passed to the query.
158158
# For most queries, this will be a Hash, but for aggregations, this
159159
# will be an Array representing the aggregation pipeline. May not be nil.
160-
# @option opts [ Integer | nil ] skip The skip value of the query.
161-
# @option opts [ Hash | nil ] sort The order of the query results
160+
# @option opts [ Integer | nil ] :skip The skip value of the query.
161+
# @option opts [ Hash | nil ] :sort The order of the query results
162162
# (e.g. { name: -1 }).
163-
# @option opts [ Integer | nil ] limit The limit value of the query.
164-
# @option opts [ Hash | nil ] projection The projection of the query
163+
# @option opts [ Integer | nil ] :limit The limit value of the query.
164+
# @option opts [ Hash | nil ] :projection The projection of the query
165165
# results (e.g. { name: 1 }).
166-
# @option opts [ Hash | nil ] collation The collation of the query
166+
# @option opts [ Hash | nil ] :collation The collation of the query
167167
# (e.g. { "locale" => "fr_CA" }).
168-
# @option opts [ Hash | nil ] read_concern The read concern of the query
168+
# @option opts [ Hash | nil ] :read_concern The read concern of the query
169169
# (e.g. { level: :majority }).
170-
# @option opts [ Hash | nil ] read_preference The read preference of
170+
# @option opts [ Hash | nil ] :read_preference The read preference of
171171
# the query (e.g. { mode: :secondary }).
172-
# @option opts [ Boolean | nil ] multi_collection Whether the query
172+
# @option opts [ Boolean | nil ] :multi_collection Whether the query
173173
# results could potentially come from multiple collections. When true,
174174
# these results will be stored under the nil namespace key and cleared
175175
# on every write command.

lib/mongo/session.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ def with_transaction(options=nil)
522522
#
523523
# @option options [ Integer ] :max_commit_time_ms The maximum amount of
524524
# time to allow a single commitTransaction command to run, in milliseconds.
525-
# @option options [ Hash ] read_concern The read concern options hash,
525+
# @option options [ Hash ] :read_concern The read concern options hash,
526526
# with the following optional keys:
527527
# - *:level* -- the read preference level as a symbol; valid values
528528
# are *:local*, *:majority*, and *:snapshot*

spec/mongo/collection_crud_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4412,4 +4412,24 @@ def generate
44124412
expect(result).to be_nil
44134413
end
44144414
end
4415+
4416+
context "when creating collection with view_on and pipeline" do
4417+
before do
4418+
authorized_client["my_view"].drop
4419+
authorized_collection.insert_one({ bar: "here!" })
4420+
authorized_client["my_view",
4421+
view_on: authorized_collection.name,
4422+
pipeline: [ { :'$project' => { "baz": "$bar" } } ]
4423+
].create
4424+
end
4425+
4426+
it "the view has a document" do
4427+
expect(authorized_client["my_view"].find.to_a.length).to eq(1)
4428+
end
4429+
4430+
it "applies the pipeline" do
4431+
expect(authorized_client["my_view"].find.first).to have_key("baz")
4432+
expect(authorized_client["my_view"].find.first["baz"]).to eq("here!")
4433+
end
4434+
end
44154435
end

spec/mongo/collection_spec.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,11 @@
647647
context 'when the collection is capped' do
648648

649649
let(:collection) do
650-
described_class.new(database, :specs, :capped => true, :size => 1024)
650+
described_class.new(database, :specs, :capped => true, :size => 4096, :max => 512)
651+
end
652+
653+
let(:collstats) do
654+
database.read_command(:collstats => :specs).documents.first
651655
end
652656

653657
before do
@@ -658,6 +662,12 @@
658662
it 'returns true' do
659663
expect(collection).to be_capped
660664
end
665+
666+
it "applies the options" do
667+
expect(collstats["capped"]).to be true
668+
expect(collstats["max"]).to eq(512)
669+
expect(collstats["maxSize"]).to eq(4096)
670+
end
661671
end
662672

663673
context 'when the collection is not capped' do

0 commit comments

Comments
 (0)