File tree Expand file tree Collapse file tree 3 files changed +70
-6
lines changed
spec/mongo/collection/view Expand file tree Collapse file tree 3 files changed +70
-6
lines changed Original file line number Diff line number Diff line change @@ -457,14 +457,14 @@ def count(filter = nil, options = {})
457457 # @param [ Hash ] filter A filter for matching documents.
458458 # @param [ Hash ] options Options for the operation.
459459 #
460- # @option opts :skip [ Integer ] The number of documents to skip.
461- # @option opts :hint [ Hash ] Override default index selection and force
460+ # @option options :skip [ Integer ] The number of documents to skip.
461+ # @option options :hint [ Hash ] Override default index selection and force
462462 # MongoDB to use a specific index for the query. Requires server version 3.6+.
463- # @option opts :limit [ Integer ] Max number of docs to count.
464- # @option opts :max_time_ms [ Integer ] The maximum amount of time to allow the
463+ # @option options :limit [ Integer ] Max number of docs to count.
464+ # @option options :max_time_ms [ Integer ] The maximum amount of time to allow the
465465 # command to run.
466- # @option opts [ Hash ] :read The read preference options.
467- # @option opts [ Hash ] :collation The collation to use.
466+ # @option options :read [ Hash ] The read preference options.
467+ # @option options :collation [ Hash ] The collation to use.
468468 #
469469 # @return [ Integer ] The document count.
470470 #
Original file line number Diff line number Diff line change @@ -152,6 +152,7 @@ def comment(comment = nil)
152152 # * $near should be replaced with $geoWithin with $center
153153 # * $nearSphere should be replaced with $geoWithin with $centerSphere
154154 def count ( opts = { } )
155+ opts = Utils . shallow_symbolize_keys ( @options . merge ( opts ) )
155156 cmd = { :count => collection . name , :query => filter }
156157 cmd [ :skip ] = opts [ :skip ] if opts [ :skip ]
157158 cmd [ :hint ] = opts [ :hint ] if opts [ :hint ]
@@ -193,11 +194,13 @@ def count(opts = {})
193194 # command to run.
194195 # @option opts [ Hash ] :read The read preference options.
195196 # @option opts [ Hash ] :collation The collation to use.
197+ # @option opts [ Mongo::Session ] :session The session to use for the operation.
196198 #
197199 # @return [ Integer ] The document count.
198200 #
199201 # @since 2.6.0
200202 def count_documents ( opts = { } )
203+ opts = Utils . shallow_symbolize_keys ( @options . merge ( opts ) )
201204 pipeline = [ :'$match' => filter ]
202205 pipeline << { :'$skip' => opts [ :skip ] } if opts [ :skip ]
203206 pipeline << { :'$limit' => opts [ :limit ] } if opts [ :limit ]
Original file line number Diff line number Diff line change 600600 end
601601 end
602602 end
603+
604+ context 'with limit' do
605+ let ( :limit ) { 6 }
606+
607+ it 'respects view limit' do
608+ expect ( view . limit ( limit ) . count ) . to eq ( limit )
609+ end
610+
611+ it 'respects limit given as parameter' do
612+ expect ( view . count ( limit : limit ) ) . to eq ( limit )
613+ end
614+ end
615+
616+ context 'with skip' do
617+ let ( :skip ) { 6 }
618+ let ( :expected ) { 4 }
619+
620+ it 'respects view skip' do
621+ expect ( view . skip ( skip ) . count ) . to eq ( expected )
622+ end
623+
624+ it 'respects skip given as parameter' do
625+ expect ( view . count ( skip : skip ) ) . to eq ( expected )
626+ end
627+ end
628+ end
629+
630+ describe '#count_documents' do
631+ let ( :documents ) do
632+ ( 1 ..10 ) . map { |i | { field : "test#{ i } " } }
633+ end
634+
635+ before do
636+ authorized_collection . delete_many
637+ authorized_collection . insert_many ( documents )
638+ end
639+
640+ context 'with limit' do
641+ let ( :limit ) { 6 }
642+
643+ it 'respects view limit' do
644+ expect ( view . limit ( limit ) . count_documents ) . to eq ( limit )
645+ end
646+
647+ it 'respects limit given as parameter' do
648+ expect ( view . count_documents ( limit : limit ) ) . to eq ( limit )
649+ end
650+ end
651+
652+ context 'with skip' do
653+ let ( :skip ) { 6 }
654+ let ( :expected ) { 4 }
655+
656+ it 'respects view skip' do
657+ expect ( view . skip ( skip ) . count_documents ) . to eq ( expected )
658+ end
659+
660+ it 'respects skip given as parameter' do
661+ expect ( view . count_documents ( skip : skip ) ) . to eq ( expected )
662+ end
663+ end
603664 end
604665
605666 describe "#estimated_document_count" do
You can’t perform that action at this time.
0 commit comments