Skip to content

MONGOID-5005 - .sum, .count, and similar aggregables should ignore sort if not limiting/skipping #5049

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/release-notes/mongoid-7.4.txt
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,17 @@ Mongoid 7.3 behavior:

Note that the alias expansion for top-level fields has already been
done by Mongoid 7.3.


``count``, ``sum``, ``avg``, ``min``, ``max`` Ignore Sort If Not Limiting/Skipping
----------------------------------------------------------------------------------

The ``count``, ``sum``, ``avg``, ``min`` and ``max`` methods now omit the
sort stage from the generated aggregation pipeline if no skip or limit
is specified, because the results aren't affected by the sort order.
Example call that will now omit the sort stage and would potentially use
an index where it wouldn't before:

.. code-block:: ruby

Band.desc(:name).count
8 changes: 5 additions & 3 deletions lib/mongoid/contextual/aggregable/mongo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,13 @@ def sum(field = nil)
# @return [ Array ] The array of pipeline operators.
def pipeline(field)
db_field = "$#{database_field_name(field)}"
sort, skip, limit = criteria.options.values_at(:sort, :skip, :limit)

pipeline = []
pipeline << { "$match" => criteria.exists(field => true).selector }
pipeline << { "$sort" => criteria.options[:sort] } if criteria.options[:sort]
pipeline << { "$skip" => criteria.options[:skip] } if criteria.options[:skip]
pipeline << { "$limit" => criteria.options[:limit] } if criteria.options[:limit]
pipeline << { "$sort" => sort } if sort && (skip || limit)
pipeline << { "$skip" => skip } if skip
pipeline << { "$limit" => limit } if limit
pipeline << {
"$group" => {
"_id" => field.to_s,
Expand Down
77 changes: 76 additions & 1 deletion spec/mongoid/contextual/aggregable/mongo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@
expect(aggregates["sum"]).to eq(1000)
end
end

end

context "when the field does not exist" do
Expand Down Expand Up @@ -517,4 +516,80 @@
end
end
end

describe '#pipeline' do
let(:context) { Mongoid::Contextual::Mongo.new(criteria) }
let(:pipeline) { context.send(:pipeline, :likes) }
subject(:stages) { pipeline.map {|s| s.keys.first } }

context "with sort" do

context "without limit or skip" do
let(:criteria) { Band.desc(:name) }

it 'should omit the $sort stage' do
expect(stages).to eq %w[$match $group]
end
end

context "with limit" do
let(:criteria) { Band.desc(:name).limit(1) }

it 'should include the $sort stage' do
expect(stages).to eq %w[$match $sort $limit $group]
end
end

context "with skip" do
let(:criteria) { Band.desc(:name).skip(1) }

it 'should include the $sort stage' do
expect(stages).to eq %w[$match $sort $skip $group]
end
end

context "with skip and skip" do
let(:criteria) { Band.desc(:name).limit(1).skip(1) }

it 'should include the $sort stage' do
expect(stages).to eq %w[$match $sort $skip $limit $group]
end
end
end

context "without sort" do

context "without limit or skip" do
let(:criteria) { Band.all }

it 'should omit the $sort stage' do
expect(stages).to eq %w[$match $group]
end
end

context "with limit" do
let(:criteria) { Band.limit(1) }

it 'should include the $sort stage' do
expect(stages).to eq %w[$match $limit $group]
end
end

context "with skip" do
let(:criteria) { Band.skip(1) }

it 'should include the $sort stage' do
expect(stages).to eq %w[$match $skip $group]
end
end

context "with skip and skip" do
let(:criteria) { Band.limit(1).skip(1) }

it 'should include the $sort stage' do
expect(stages).to eq %w[$match $skip $limit $group]
end
end
end
end
end