Skip to content

Commit

Permalink
MONGOID-5103 Implement eq symbol operator (#5076)
Browse files Browse the repository at this point in the history
  • Loading branch information
comandeo-mongo authored Sep 13, 2021
1 parent 55b8837 commit 6ad02bf
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/mongoid/criteria/queryable/selectable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,26 @@ def geo_spacial(criterion)
end
key :within_box, :override, "$geoWithin", "$box"

# Add the $eq criterion to the selector.
#
# @example Add the $eq criterion.
# selectable.eq(age: 60)
#
# @example Execute an $eq in a where query.
# selectable.where(:field.eq => 10)
#
# @param [ Hash ] criterion The field/value pairs to check.
#
# @return [ Selectable ] The cloned selectable.
def eq(criterion)
if criterion.nil?
raise Errors::CriteriaArgumentRequired, :eq
end

__override__(criterion, "$eq")
end
key :eq, :override, "$eq"

# Add the $gt criterion to the selector.
#
# @example Add the $gt criterion.
Expand Down
19 changes: 19 additions & 0 deletions spec/mongoid/criteria_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,25 @@
end
end

describe "#eq" do

let!(:match) do
Band.create(member_count: 5)
end

let!(:non_match) do
Band.create(member_count: 1)
end

let(:criteria) do
Band.eq(member_count: 5)
end

it "returns the matching documents" do
expect(criteria).to eq([ match ])
end
end

describe "#gt" do

let!(:match) do
Expand Down

0 comments on commit 6ad02bf

Please sign in to comment.