forked from mongodb/mongo-ruby-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollection.rb
More file actions
1308 lines (1251 loc) · 57.4 KB
/
collection.rb
File metadata and controls
1308 lines (1251 loc) · 57.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# frozen_string_literal: true
# rubocop:todo all
# Copyright (C) 2014-2020 MongoDB Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require 'mongo/bulk_write'
require 'mongo/collection/view'
require 'mongo/collection/helpers'
require 'mongo/collection/queryable_encryption'
module Mongo
# Represents a collection in the database and operations that can directly be
# applied to one.
#
# @since 2.0.0
class Collection
extend Forwardable
include Retryable
include QueryableEncryption
include Helpers
# The capped option.
#
# @since 2.1.0
CAPPED = 'capped'.freeze
# The ns field constant.
#
# @since 2.1.0
NS = 'ns'.freeze
# @return [ Mongo::Database ] The database the collection resides in.
attr_reader :database
# @return [ String ] The name of the collection.
attr_reader :name
# @return [ Hash ] The collection options.
attr_reader :options
# Get client, cluster, read preference, write concern, and encrypted_fields_map from client.
def_delegators :database, :client, :cluster, :encrypted_fields_map
# Delegate to the cluster for the next primary.
def_delegators :cluster, :next_primary
def_delegators :client, :tracer
# Options that can be updated on a new Collection instance via the #with method.
#
# @since 2.1.0
CHANGEABLE_OPTIONS = [ :read, :read_concern, :write, :write_concern ].freeze
# Options map to transform create collection options.
#
# @api private
CREATE_COLLECTION_OPTIONS = {
:time_series => :timeseries,
:expire_after => :expireAfterSeconds,
:clustered_index => :clusteredIndex,
:change_stream_pre_and_post_images => :changeStreamPreAndPostImages,
:encrypted_fields => :encryptedFields,
:validator => :validator,
:view_on => :viewOn
}
# Check if a collection is equal to another object. Will check the name and
# the database for equality.
#
# @example Check collection equality.
# collection == other
#
# @param [ Object ] other The object to check.
#
# @return [ true | false ] If the objects are equal.
#
# @since 2.0.0
def ==(other)
return false unless other.is_a?(Collection)
name == other.name && database == other.database && options == other.options
end
# Instantiate a new collection.
#
# @example Instantiate a new collection.
# Mongo::Collection.new(database, 'test')
#
# @param [ Mongo::Database ] database The collection's database.
# @param [ String, Symbol ] name The collection name.
# @param [ Hash ] options The collection options.
#
# @option opts [ true | false ] :capped Create a fixed-sized collection.
# @option opts [ Hash ] :change_stream_pre_and_post_images Used to enable
# pre- and post-images on the created collection.
# The hash may have the following items:
# - *:enabled* -- true or false.
# @option opts [ Hash ] :clustered_index Create a clustered index.
# This option specifies how this collection should be clustered on _id.
# The hash may have the following items:
# - *:key* -- The clustered index key field. Must be set to { _id: 1 }.
# - *:unique* -- Must be set to true. The collection will not accept
# inserted or updated documents where the clustered index key value
# matches an existing value in the index.
# - *:name* -- Optional. A name that uniquely identifies the clustered index.
# @option opts [ Hash ] :collation The collation to use.
# @option opts [ Hash ] :encrypted_fields Hash describing encrypted fields
# for queryable encryption.
# @option opts [ Integer ] :expire_after Number indicating
# after how many seconds old time-series data should be deleted.
# @option opts [ Integer ] :max The maximum number of documents in a
# capped collection. The size limit takes precedents over max.
# @option opts [ Array<Hash> ] :pipeline An array of pipeline stages.
# A view will be created by applying this pipeline to the view_on
# collection or view.
# @option options [ Hash ] :read_concern The read concern options hash,
# with the following optional keys:
# - *:level* -- the read preference level as a symbol; valid values
# are *:local*, *:majority*, and *:snapshot*
# @option options [ Hash ] :read The read preference options.
# The hash may have the following items:
# - *:mode* -- read preference specified as a symbol; valid values are
# *:primary*, *:primary_preferred*, *:secondary*, *:secondary_preferred*
# and *:nearest*.
# - *:tag_sets* -- an array of hashes.
# - *:local_threshold*.
# @option options [ Session ] :session The session to use for the operation.
# @option options [ Integer ] :size The size of the capped collection.
# @option options [ Integer ] :timeout_ms The operation timeout in milliseconds.
# Must be a non-negative integer. An explicit value of 0 means infinite.
# The default value is unset which means the value is inherited from
# the database or the client.
# @option opts [ Hash ] :time_series Create a time-series collection.
# The hash may have the following items:
# - *:timeField* -- The name of the field which contains the date in each
# time series document.
# - *:metaField* -- The name of the field which contains metadata in each
# time series document.
# - *:granularity* -- Set the granularity to the value that is the closest
# match to the time span between consecutive incoming measurements.
# Possible values are "seconds" (default), "minutes", and "hours".
# @option opts [ Hash ] :validator Hash describing document validation
# options for the collection.
# @option opts [ String ] :view_on The name of the source collection or
# view from which to create a view.
# @option opts [ Hash ] :write Deprecated. Equivalent to :write_concern
# option.
# @option opts [ Hash ] :write_concern The write concern options.
# Can be :w => Integer|String, :fsync => Boolean, :j => Boolean.
#
# @since 2.0.0
def initialize(database, name, options = {})
raise Error::InvalidCollectionName.new unless name
if options[:write] && options[:write_concern] && options[:write] != options[:write_concern]
raise ArgumentError, "If :write and :write_concern are both given, they must be identical: #{options.inspect}"
end
@database = database
@name = name.to_s.freeze
@options = options.dup
@timeout_ms = options.delete(:timeout_ms)
=begin WriteConcern object support
if @options[:write_concern].is_a?(WriteConcern::Base)
# Cache the instance so that we do not needlessly reconstruct it.
@write_concern = @options[:write_concern]
@options[:write_concern] = @write_concern.options
end
=end
@options.freeze
end
# Get the effective read concern for this collection instance.
#
# If a read concern was provided in collection options, that read concern
# will be returned, otherwise the database's effective read concern will
# be returned.
#
# @example Get the read concern.
# collection.read_concern
#
# @return [ Hash ] The read concern.
#
# @since 2.2.0
def read_concern
options[:read_concern] || database.read_concern
end
# Get the server selector for this collection.
#
# @example Get the server selector.
# collection.server_selector
#
# @return [ Mongo::ServerSelector ] The server selector.
#
# @since 2.0.0
def server_selector
@server_selector ||= ServerSelector.get(read_preference || database.server_selector)
end
# Get the effective read preference for this collection.
#
# If a read preference was provided in collection options, that read
# preference will be returned, otherwise the database's effective read
# preference will be returned.
#
# @example Get the read preference.
# collection.read_preference
#
# @return [ Hash ] The read preference.
#
# @since 2.0.0
def read_preference
@read_preference ||= options[:read] || database.read_preference
end
# Get the effective write concern on this collection.
#
# If a write concern was provided in collection options, that write
# concern will be returned, otherwise the database's effective write
# concern will be returned.
#
# @example Get the write concern.
# collection.write_concern
#
# @return [ Mongo::WriteConcern ] The write concern.
#
# @since 2.0.0
def write_concern
@write_concern ||= WriteConcern.get(
options[:write_concern] || options[:write] || database.write_concern)
end
# Get the write concern to use for an operation on this collection,
# given a session.
#
# If the session is in a transaction and the collection
# has an unacknowledged write concern, remove the write
# concern's :w option. Otherwise, return the unmodified
# write concern.
#
# @return [ Mongo::WriteConcern ] The write concern.
#
# @api private
def write_concern_with_session(session)
wc = write_concern
if session && session.in_transaction?
if wc && !wc.acknowledged?
opts = wc.options.dup
opts.delete(:w)
return WriteConcern.get(opts)
end
end
wc
end
# Provides a new collection with either a new read preference, new read
# concern or new write concern merged over the existing read preference /
# read concern / write concern.
#
# @example Get a collection with a changed read preference.
# collection.with(read: { mode: :primary_preferred })
# @example Get a collection with a changed read concern.
# collection.with(read_concern: { level: :majority })
#
# @example Get a collection with a changed write concern.
# collection.with(write_concern: { w: 3 })
#
# @param [ Hash ] new_options The new options to use.
#
# @option new_options [ Hash ] :read The read preference options.
# The hash may have the following items:
# - *:mode* -- read preference specified as a symbol; valid values are
# *:primary*, *:primary_preferred*, *:secondary*, *:secondary_preferred*
# and *:nearest*.
# - *:tag_sets* -- an array of hashes.
# - *:local_threshold*.
# @option new_options [ Hash ] :read_concern The read concern options hash,
# with the following optional keys:
# - *:level* -- the read preference level as a symbol; valid values
# are *:local*, *:majority*, and *:snapshot*
# @option new_options [ Hash ] :write Deprecated. Equivalent to :write_concern
# option.
# @option new_options [ Hash ] :write_concern The write concern options.
# Can be :w => Integer|String, :fsync => Boolean, :j => Boolean.
#
# @return [ Mongo::Collection ] A new collection instance.
#
# @since 2.1.0
def with(new_options)
new_options.keys.each do |k|
raise Error::UnchangeableCollectionOption.new(k) unless CHANGEABLE_OPTIONS.include?(k)
end
options = @options.dup
if options[:write] && new_options[:write_concern]
options.delete(:write)
end
if options[:write_concern] && new_options[:write]
options.delete(:write_concern)
end
Collection.new(database, name, options.update(new_options))
end
# Is the collection capped?
#
# @example Is the collection capped?
# collection.capped?
#
# @return [ true | false ] If the collection is capped.
#
# @since 2.0.0
def capped?
database.list_collections(filter: { name: name })
.first
&.dig('options', CAPPED) || false
end
# Force the collection to be created in the database.
#
# @example Force the collection to be created.
# collection.create
#
# @param [ Hash ] opts The options for the create operation.
#
# @option opts [ true | false ] :capped Create a fixed-sized collection.
# @option opts [ Hash ] :change_stream_pre_and_post_images Used to enable
# pre- and post-images on the created collection.
# The hash may have the following items:
# - *:enabled* -- true or false.
# @option opts [ Hash ] :clustered_index Create a clustered index.
# This option specifies how this collection should be clustered on _id.
# The hash may have the following items:
# - *:key* -- The clustered index key field. Must be set to { _id: 1 }.
# - *:unique* -- Must be set to true. The collection will not accept
# inserted or updated documents where the clustered index key value
# matches an existing value in the index.
# - *:name* -- Optional. A name that uniquely identifies the clustered index.
# @option opts [ Hash ] :collation The collation to use when creating the
# collection. This option will not be sent to the server when calling
# collection methods.
# @option opts [ Hash ] :encrypted_fields Hash describing encrypted fields
# for queryable encryption.
# @option opts [ Integer ] :expire_after Number indicating
# after how many seconds old time-series data should be deleted.
# @option opts [ Integer ] :max The maximum number of documents in a
# capped collection. The size limit takes precedents over max.
# @option opts [ Array<Hash> ] :pipeline An array of pipeline stages.
# A view will be created by applying this pipeline to the view_on
# collection or view.
# @option opts [ Session ] :session The session to use for the operation.
# @option opts [ Integer ] :size The size of the capped collection.
# @option opts [ Hash ] :time_series Create a time-series collection.
# The hash may have the following items:
# - *:timeField* -- The name of the field which contains the date in each
# time series document.
# - *:metaField* -- The name of the field which contains metadata in each
# time series document.
# - *:granularity* -- Set the granularity to the value that is the closest
# match to the time span between consecutive incoming measurements.
# Possible values are "seconds" (default), "minutes", and "hours".
# @option opts [ Hash ] :validator Hash describing document validation
# options for the collection.
# @option opts [ String ] :view_on The name of the source collection or
# view from which to create a view.
# @option opts [ Hash ] :write Deprecated. Equivalent to :write_concern
# option.
# @option opts [ Hash ] :write_concern The write concern options.
# Can be :w => Integer|String, :fsync => Boolean, :j => Boolean.
#
# @return [ Result ] The result of the command.
#
# @since 2.0.0
def create(opts = {})
# Passing read options to create command causes it to break.
# Filter the read options out. Session is also excluded here as it gets
# used by the call to with_session and should not be part of the
# operation. If it gets passed to the operation it would fail BSON
# serialization.
# TODO put the list of read options in a class-level constant when
# we figure out what the full set of them is.
options = Hash[self.options.merge(opts).reject do |key, value|
%w(read read_preference read_concern session).include?(key.to_s)
end]
# Converting Ruby options to server style.
CREATE_COLLECTION_OPTIONS.each do |ruby_key, server_key|
if options.key?(ruby_key)
options[server_key] = options.delete(ruby_key)
end
end
operation = { :create => name }.merge(options)
operation.delete(:write)
operation.delete(:write_concern)
client.send(:with_session, opts) do |session|
write_concern = if opts[:write_concern]
WriteConcern.get(opts[:write_concern])
else
self.write_concern
end
context = Operation::Context.new(
client: client,
session: session
)
operation = Operation::Create.new(
selector: operation,
db_name: database.name,
write_concern: write_concern,
session: session,
# Note that these are collection options, collation isn't
# taken from options passed to the create method.
collation: options[:collation] || options['collation'],
validator: options[:validator],
)
tracer.trace_operation(operation, context, op_name: 'createCollection') do
maybe_create_qe_collections(opts[:encrypted_fields], client, session) do |encrypted_fields|
operation.encrypted_fields = encrypted_fields
operation.execute(
next_primary(nil, session),
context: context
)
end
end
end
end
# Drop the collection. Will also drop all indexes associated with the
# collection, as well as associated queryable encryption collections.
#
# @note An error returned if the collection doesn't exist is suppressed.
#
# @example Drop the collection.
# collection.drop
#
# @param [ Hash ] opts The options for the drop operation.
#
# @option opts [ Session ] :session The session to use for the operation.
# @option opts [ Hash ] :write_concern The write concern options.
# @option opts [ Hash | nil ] :encrypted_fields Encrypted fields hash that
# was provided to `create` collection helper.
# @option options [ Integer ] :timeout_ms The operation timeout in milliseconds.
# Must be a non-negative integer. An explicit value of 0 means infinite.
# The default value is unset which means the value is inherited from
# the collection or the database or the client.
#
# @return [ Result ] The result of the command.
#
# @since 2.0.0
def drop(opts = {})
client.with_session(opts) do |session|
context = Operation::Context.new(
client: client,
session: session,
operation_timeouts: operation_timeouts(opts)
)
temp_write_concern = write_concern
write_concern = if opts[:write_concern]
WriteConcern.get(opts[:write_concern])
else
temp_write_concern
end
operation = Operation::Drop.new({
selector: { :drop => name },
db_name: database.name,
write_concern: write_concern,
session: session,
})
tracer.trace_operation(operation, context, op_name: 'dropCollection') do
maybe_drop_emm_collections(opts[:encrypted_fields], client, session) do
do_drop(operation, session, context)
end
end
end
end
# Find documents in the collection.
#
# @example Find documents in the collection by a selector.
# collection.find(name: 1)
#
# @example Get all documents in a collection.
# collection.find
#
# @param [ Hash ] filter The filter to use in the find.
# @param [ Hash ] options The options for the find.
#
# @option options [ true | false ] :allow_disk_use When set to true, the
# server can write temporary data to disk while executing the find
# operation. This option is only available on MongoDB server versions
# 4.4 and newer.
# @option options [ true | false ] :allow_partial_results Allows the query to get partial
# results if some shards are down.
# @option options [ Integer ] :batch_size The number of documents returned in each batch
# of results from MongoDB.
# @option options [ Hash ] :collation The collation to use.
# @option options [ Object ] :comment A user-provided comment to attach to
# this command.
# @option options [ :tailable, :tailable_await ] :cursor_type The type of cursor to use.
# @option options [ Integer ] :limit The max number of docs to return from the query.
# @option options [ Integer ] :max_time_ms The maximum amount of time to
# allow the query to run, in milliseconds. This option is deprecated, use
# :timeout_ms instead.
# @option options [ Hash ] :modifiers A document containing meta-operators modifying the
# output or behavior of a query.
# @option options [ true | false ] :no_cursor_timeout The server normally times out idle
# cursors after an inactivity period (10 minutes) to prevent excess memory use.
# Set this option to prevent that.
# @option options [ true | false ] :oplog_replay For internal replication
# use only, applications should not set this option.
# @option options [ Hash ] :projection The fields to include or exclude from each doc
# in the result set.
# @option options [ Session ] :session The session to use.
# @option options [ Integer ] :skip The number of docs to skip before returning results.
# @option options [ Hash ] :sort The key and direction pairs by which the result set
# will be sorted.
# @option options [ :cursor_lifetime | :iteration ] :timeout_mode How to interpret
# :timeout_ms (whether it applies to the lifetime of the cursor, or per
# iteration).
# @option options [ Integer ] :timeout_ms The operation timeout in milliseconds.
# Must be a non-negative integer. An explicit value of 0 means infinite.
# The default value is unset which means the value is inherited from
# the collection or the database or the client.
# @option options [ Hash ] :let Mapping of variables to use in the command.
# See the server documentation for details.
#
# @return [ CollectionView ] The collection view.
#
# @since 2.0.0
def find(filter = nil, options = {})
View.new(self, filter || {}, options)
end
# Perform an aggregation on the collection.
#
# @example Perform an aggregation.
# collection.aggregate([ { "$group" => { "_id" => "$city", "tpop" => { "$sum" => "$pop" }}} ])
#
# @param [ Array<Hash> ] pipeline The aggregation pipeline.
# @param [ Hash ] options The aggregation options.
#
# @option options [ true | false ] :allow_disk_use Set to true if disk
# usage is allowed during the aggregation.
# @option options [ Integer ] :batch_size The number of documents to return
# per batch.
# @option options [ true | false ] :bypass_document_validation Whether or
# not to skip document level validation.
# @option options [ Hash ] :collation The collation to use.
# @option options [ Object ] :comment A user-provided
# comment to attach to this command.
# @option options [ String ] :hint The index to use for the aggregation.
# @option options [ Hash ] :let Mapping of variables to use in the pipeline.
# See the server documentation for details.
# @option options [ Integer ] :max_time_ms The maximum amount of time to
# allow the query to run, in milliseconds. This option is deprecated, use
# :timeout_ms instead.
# @option options [ Session ] :session The session to use.
# @option options [ Integer ] :timeout_ms The operation timeout in milliseconds.
# Must be a non-negative integer. An explicit value of 0 means infinite.
# The default value is unset which means the value is inherited from
# the collection or the database or the client.
#
# @return [ View::Aggregation ] The aggregation object.
#
# @since 2.1.0
def aggregate(pipeline, options = {})
View.new(self, {}, options).aggregate(pipeline, options)
end
# Allows users to request that notifications are sent for all changes
# to a particular collection.
#
# @example Get change notifications for a given collection.
# collection.watch([{ '$match' => { operationType: { '$in' => ['insert', 'replace'] } } }])
#
# @param [ Array<Hash> ] pipeline Optional additional filter operators.
# @param [ Hash ] options The change stream options.
#
# @option options [ String ] :full_document Allowed values: nil, 'default',
# 'updateLookup', 'whenAvailable', 'required'.
#
# The default is to not send a value (i.e. nil), which is equivalent to
# 'default'. By default, the change notification for partial updates will
# include a delta describing the changes to the document.
#
# When set to 'updateLookup', the change notification for partial updates
# will include both a delta describing the changes to the document as well
# as a copy of the entire document that was changed from some time after
# the change occurred.
#
# When set to 'whenAvailable', configures the change stream to return the
# post-image of the modified document for replace and update change events
# if the post-image for this event is available.
#
# When set to 'required', the same behavior as 'whenAvailable' except that
# an error is raised if the post-image is not available.
# @option options [ String ] :full_document_before_change Allowed values: nil,
# 'whenAvailable', 'required', 'off'.
#
# The default is to not send a value (i.e. nil), which is equivalent to 'off'.
#
# When set to 'whenAvailable', configures the change stream to return the
# pre-image of the modified document for replace, update, and delete change
# events if it is available.
#
# When set to 'required', the same behavior as 'whenAvailable' except that
# an error is raised if the pre-image is not available.
# @option options [ BSON::Document, Hash ] :resume_after Specifies the
# logical starting point for the new change stream.
# @option options [ Integer ] :max_await_time_ms The maximum amount of time
# for the server to wait on new documents to satisfy a change stream query.
# @option options [ Integer ] :batch_size The number of documents to return
# per batch.
# @option options [ BSON::Document, Hash ] :collation The collation to use.
# @option options [ Session ] :session The session to use.
# @option options [ BSON::Timestamp ] :start_at_operation_time Only return
# changes that occurred at or after the specified timestamp. Any command run
# against the server will return a cluster time that can be used here.
# @option options [ Object ] :comment A user-provided
# comment to attach to this command.
# @option options [ Boolean ] :show_expanded_events Enables the server to
# send the 'expanded' list of change stream events. The list of additional
# events included with this flag set are: createIndexes, dropIndexes,
# modify, create, shardCollection, reshardCollection,
# refineCollectionShardKey.
# @option options [ :cursor_lifetime | :iteration ] :timeout_mode How to interpret
# :timeout_ms (whether it applies to the lifetime of the cursor, or per
# iteration).
# @option options [ Integer ] :timeout_ms The operation timeout in milliseconds.
# Must be a non-negative integer. An explicit value of 0 means infinite.
# The default value is unset which means the value is inherited from
# the collection or the database or the client.
#
# @note A change stream only allows 'majority' read concern.
# @note This helper method is preferable to running a raw aggregation with
# a $changeStream stage, for the purpose of supporting resumability.
#
# @return [ ChangeStream ] The change stream object.
#
# @since 2.5.0
def watch(pipeline = [], options = {})
view_options = options.dup
view_options[:cursor_type] = :tailable_await if options[:max_await_time_ms]
View::ChangeStream.new(View.new(self, {}, view_options), pipeline, nil, options)
end
# Gets an estimated number of matching documents in the collection.
#
# @example Get the count.
# collection.count(name: 1)
#
# @param [ Hash ] filter A filter for matching documents.
# @param [ Hash ] options The count options.
#
# @option options [ Hash ] :hint The index to use.
# @option options [ Integer ] :limit The maximum number of documents to count.
# @option options [ Integer ] :max_time_ms The maximum amount of time to
# allow the query to run, in milliseconds. This option is deprecated, use
# :timeout_ms instead.
# @option options [ Integer ] :skip The number of documents to skip before counting.
# @option options [ Hash ] :read The read preference options.
# @option options [ Hash ] :collation The collation to use.
# @option options [ Session ] :session The session to use.
# @option options [ Object ] :comment A user-provided
# comment to attach to this command.
# @option options [ Integer ] :timeout_ms The operation timeout in milliseconds.
# Must be a non-negative integer. An explicit value of 0 means infinite.
# The default value is unset which means the value is inherited from
# the collection or the database or the client.
#
# @return [ Integer ] The document count.
#
# @since 2.1.0
#
# @deprecated Use #count_documents or estimated_document_count instead. However, note that the
# following operators will need to be substituted when switching to #count_documents:
# * $where should be replaced with $expr
# * $near should be replaced with $geoWithin with $center
# * $nearSphere should be replaced with $geoWithin with $centerSphere
def count(filter = nil, options = {})
View.new(self, filter || {}, options).count(options)
end
# Gets the number of documents matching the query. Unlike the deprecated
# #count method, this will return the exact number of documents matching
# the filter (or exact number of documents in the collection, if no filter
# is provided) rather than an estimate.
#
# Use #estimated_document_count to retrieve an estimate of the number
# of documents in the collection using the collection metadata.
#
# @param [ Hash ] filter A filter for matching documents.
# @param [ Hash ] options Options for the operation.
#
# @option options :skip [ Integer ] The number of documents to skip.
# @option options :hint [ Hash ] Override default index selection and force
# MongoDB to use a specific index for the query.
# @option options :limit [ Integer ] Max number of docs to count.
# @option options :max_time_ms [ Integer ] The maximum amount of time to allow the
# command to run.
# @option options :read [ Hash ] The read preference options.
# @option options :collation [ Hash ] The collation to use.
# @option options [ Session ] :session The session to use.
# @option options [ Object ] :comment A user-provided
# comment to attach to this command.
# @option options [ Integer ] :timeout_ms The operation timeout in milliseconds.
# Must be a non-negative integer. An explicit value of 0 means infinite.
# The default value is unset which means the value is inherited from
# the collection or the database or the client.
#
# @return [ Integer ] The document count.
#
# @since 2.6.0
def count_documents(filter = {}, options = {})
View.new(self, filter, options).count_documents(options)
end
# Gets an estimate of the number of documents in the collection using the
# collection metadata.
#
# Use #count_documents to retrieve the exact number of documents in the
# collection, or to count documents matching a filter.
#
# @param [ Hash ] options Options for the operation.
#
# @option options :max_time_ms [ Integer ] The maximum amount of time to allow
# the command to run for on the server.
# @option options [ Hash ] :read The read preference options.
# @option options [ Object ] :comment A user-provided
# comment to attach to this command.
# @option options [ Integer ] :timeout_ms The operation timeout in milliseconds.
# Must be a non-negative integer. An explicit value of 0 means infinite.
# The default value is unset which means the value is inherited from
# the collection or the database or the client.
#
# @return [ Integer ] The document count.
#
# @since 2.6.0
def estimated_document_count(options = {})
View.new(self, {}, options).estimated_document_count(options)
end
# Get a list of distinct values for a specific field.
#
# @example Get the distinct values.
# collection.distinct('name')
#
# @param [ Symbol, String ] field_name The name of the field.
# @param [ Hash ] filter The documents from which to retrieve the distinct values.
# @param [ Hash ] options The distinct command options.
#
# @option options [ Integer ] :max_time_ms The maximum amount of time to
# allow the query to run, in milliseconds. This option is deprecated, use
# :timeout_ms instead.
# @option options [ Hash ] :read The read preference options.
# @option options [ Hash ] :collation The collation to use.
# @option options [ Session ] :session The session to use.
# @option options [ Integer ] :timeout_ms The operation timeout in milliseconds.
# Must be a non-negative integer. An explicit value of 0 means infinite.
# The default value is unset which means the value is inherited from
# the collection or the database or the client.
#
# @return [ Array<Object> ] The list of distinct values.
#
# @since 2.1.0
def distinct(field_name, filter = nil, options = {})
View.new(self, filter || {}, options).distinct(field_name, options)
end
# Get a view of all indexes for this collection. Can be iterated or has
# more operations.
#
# @example Get the index view.
# collection.indexes
#
# @param [ Hash ] options Options for getting a list of all indexes.
#
# @option options [ Session ] :session The session to use.
#
# @return [ Index::View ] The index view.
#
# @since 2.0.0
def indexes(options = {})
Index::View.new(self, options)
end
# Get a view of all search indexes for this collection. Can be iterated or
# operated on directly. If id or name are given, the iterator will return
# only the indicated index. For all other operations, id and name are
# ignored.
#
# @note Only one of id or name may be given; it is an error to specify both,
# although both may be omitted safely.
#
# @param [ Hash ] options The options to use to configure the view.
#
# @option options [ String ] :id The id of the specific index to query (optional)
# @option options [ String ] :name The name of the specific index to query (optional)
# @option options [ Hash ] :aggregate The options hash to pass to the
# aggregate command (optional)
#
# @return [ SearchIndex::View ] The search index view.
#
# @since 2.0.0
def search_indexes(options = {})
SearchIndex::View.new(self, options)
end
# Get a pretty printed string inspection for the collection.
#
# @example Inspect the collection.
# collection.inspect
#
# @return [ String ] The collection inspection.
#
# @since 2.0.0
def inspect
"#<Mongo::Collection:0x#{object_id} namespace=#{namespace}>"
end
# Insert a single document into the collection.
#
# @example Insert a document into the collection.
# collection.insert_one({ name: 'test' })
#
# @param [ Hash ] document The document to insert.
# @param [ Hash ] opts The insert options.
#
# @option opts [ true | false ] :bypass_document_validation Whether or
# not to skip document level validation.
# @option opts [ Object ] :comment A user-provided comment to attach to
# this command.
# @option opts [ Session ] :session The session to use for the operation.
# @option options [ Integer ] :timeout_ms The operation timeout in milliseconds.
# Must be a non-negative integer. An explicit value of 0 means infinite.
# The default value is unset which means the value is inherited from
# the collection or the database or the client.
# @option opts [ Hash ] :write_concern The write concern options.
# Can be :w => Integer, :fsync => Boolean, :j => Boolean.
#
# @return [ Result ] The database response wrapper.
#
# @since 2.0.0
def insert_one(document, opts = {})
QueryCache.clear_namespace(namespace)
client.with_session(opts) do |session|
write_concern = if opts[:write_concern]
WriteConcern.get(opts[:write_concern])
else
write_concern_with_session(session)
end
if document.nil?
raise ArgumentError, "Document to be inserted cannot be nil"
end
context = Operation::Context.new(
client: client,
session: session,
operation_timeouts: operation_timeouts(opts)
)
operation = Operation::Insert.new(
:documents => [ document ],
:db_name => database.name,
:coll_name => name,
:write_concern => write_concern,
:bypass_document_validation => !!opts[:bypass_document_validation],
:options => opts,
:id_generator => client.options[:id_generator],
:session => session,
:comment => opts[:comment]
)
tracer.trace_operation(operation, context) do
write_with_retry(write_concern, context: context) do |connection, txn_num, context|
operation.txn_num = txn_num
operation.execute_with_connection(connection, context: context)
end
end
end
end
# Insert the provided documents into the collection.
#
# @example Insert documents into the collection.
# collection.insert_many([{ name: 'test' }])
#
# @param [ Enumerable<Hash> ] documents The documents to insert.
# @param [ Hash ] options The insert options.
#
# @option options [ true | false ] :bypass_document_validation Whether or
# not to skip document level validation.
# @option options [ Object ] :comment A user-provided comment to attach to
# this command.
# @option options [ true | false ] :ordered Whether the operations
# should be executed in order.
# @option options [ Session ] :session The session to use for the operation.
# @option options [ Integer ] :timeout_ms The operation timeout in milliseconds.
# Must be a non-negative integer. An explicit value of 0 means infinite.
# The default value is unset which means the value is inherited from
# the collection or the database or the client.
# @option options [ Hash ] :write_concern The write concern options.
# Can be :w => Integer, :fsync => Boolean, :j => Boolean.
#
# @return [ Result ] The database response wrapper.
#
# @since 2.0.0
def insert_many(documents, options = {})
QueryCache.clear_namespace(namespace)
inserts = documents.map{ |doc| { :insert_one => doc }}
bulk_write(inserts, options)
end
# Execute a batch of bulk write operations.
#
# @example Execute a bulk write.
# collection.bulk_write(operations, options)
#
# @param [ Enumerable<Hash> ] requests The bulk write requests.
# @param [ Hash ] options The options.
#
# @option options [ true | false ] :ordered Whether the operations
# should be executed in order.
# @option options [ Hash ] :write_concern The write concern options.
# Can be :w => Integer, :fsync => Boolean, :j => Boolean.
# @option options [ true | false ] :bypass_document_validation Whether or
# not to skip document level validation.
# @option options [ Session ] :session The session to use for the set of operations.
# @option options [ Integer ] :timeout_ms The operation timeout in milliseconds.
# Must be a non-negative integer. An explicit value of 0 means infinite.
# The default value is unset which means the value is inherited from
# the collection or the database or the client.
# @option options [ Hash ] :let Mapping of variables to use in the command.
# See the server documentation for details.
#
# @return [ BulkWrite::Result ] The result of the operation.
#
# @since 2.0.0
def bulk_write(requests, options = {})
BulkWrite.new(self, requests, options).execute
end
# Remove a document from the collection.
#
# @example Remove a single document from the collection.
# collection.delete_one
#
# @param [ Hash ] filter The filter to use.
# @param [ Hash ] options The options.
#
# @option options [ Hash ] :collation The collation to use.
# @option options [ Session ] :session The session to use.
# @option options [ Hash | String ] :hint The index to use for this operation.
# May be specified as a Hash (e.g. { _id: 1 }) or a String (e.g. "_id_").
# @option options [ Integer ] :timeout_ms The operation timeout in milliseconds.
# Must be a non-negative integer. An explicit value of 0 means infinite.
# The default value is unset which means the value is inherited from
# the collection or the database or the client.
# @option options [ Hash ] :let Mapping of variables to use in the command.
# See the server documentation for details.
#
# @return [ Result ] The response from the database.
#
# @since 2.1.0
def delete_one(filter = nil, options = {})
find(filter, options).delete_one(options)
end
# Remove documents from the collection.
#
# @example Remove multiple documents from the collection.
# collection.delete_many
#
# @param [ Hash ] filter The filter to use.
# @param [ Hash ] options The options.
#
# @option options [ Hash ] :collation The collation to use.
# @option options [ Session ] :session The session to use.
# @option options [ Hash | String ] :hint The index to use for this operation.
# May be specified as a Hash (e.g. { _id: 1 }) or a String (e.g. "_id_").
# @option options [ Integer ] :timeout_ms The operation timeout in milliseconds.
# Must be a non-negative integer. An explicit value of 0 means infinite.
# The default value is unset which means the value is inherited from
# the collection or the database or the client.
# @option options [ Hash ] :let Mapping of variables to use in the command.
# See the server documentation for details.
#
# @return [ Result ] The response from the database.
#
# @since 2.1.0