From 6ad02bf87d507643190863012dfd861fba7da9e9 Mon Sep 17 00:00:00 2001 From: Dmitry Rybakov Date: Mon, 13 Sep 2021 09:56:14 +0200 Subject: [PATCH] MONGOID-5103 Implement eq symbol operator (#5076) --- lib/mongoid/criteria/queryable/selectable.rb | 20 ++++++++++++++++++++ spec/mongoid/criteria_spec.rb | 19 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/lib/mongoid/criteria/queryable/selectable.rb b/lib/mongoid/criteria/queryable/selectable.rb index 736e5845cd..fc26e55868 100644 --- a/lib/mongoid/criteria/queryable/selectable.rb +++ b/lib/mongoid/criteria/queryable/selectable.rb @@ -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. diff --git a/spec/mongoid/criteria_spec.rb b/spec/mongoid/criteria_spec.rb index 475d6a473d..fa5e5e3689 100644 --- a/spec/mongoid/criteria_spec.rb +++ b/spec/mongoid/criteria_spec.rb @@ -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