|
3746 | 3746 | expect(new_order.reload.genres).to eq(["electronic"])
|
3747 | 3747 | end
|
3748 | 3748 | end
|
| 3749 | + |
| 3750 | + context "when operation is $pull" do |
| 3751 | + context "when pulling single element" do |
| 3752 | + |
| 3753 | + before do |
| 3754 | + depeche_mode.update_attribute(:genres, ["electronic", "pop"]) |
| 3755 | + new_order.update_attribute(:genres, ["electronic", "pop"]) |
| 3756 | + context.update_all("$pull" => { genres: "electronic" }) |
| 3757 | + end |
| 3758 | + |
| 3759 | + it "updates the first matching document" do |
| 3760 | + expect(depeche_mode.reload.genres).to eq(["pop"]) |
| 3761 | + end |
| 3762 | + |
| 3763 | + it "updates the last matching document" do |
| 3764 | + expect(new_order.reload.genres).to eq(["pop"]) |
| 3765 | + end |
| 3766 | + end |
| 3767 | + |
| 3768 | + context "when pulling based on condition" do |
| 3769 | + before do |
| 3770 | + depeche_mode.update_attribute(:genres, ["electronic", "pop", "dance"]) |
| 3771 | + new_order.update_attribute(:genres, ["electronic", "pop", "dance"]) |
| 3772 | + context.update_all("$pull" => { genres: { '$in' => ["electronic", "pop"] } }) |
| 3773 | + end |
| 3774 | + |
| 3775 | + it "updates the first matching document" do |
| 3776 | + expect(depeche_mode.reload.genres).to eq(["dance"]) |
| 3777 | + end |
| 3778 | + |
| 3779 | + it "updates the last matching document" do |
| 3780 | + expect(new_order.reload.genres).to eq(["dance"]) |
| 3781 | + end |
| 3782 | + end |
| 3783 | + end |
| 3784 | + |
| 3785 | + context "when operation is $pop" do |
| 3786 | + |
| 3787 | + before do |
| 3788 | + depeche_mode.update_attribute(:genres, ["pop", "electronic"]) |
| 3789 | + end |
| 3790 | + |
| 3791 | + it "removes first element in array" do |
| 3792 | + context.update_all("$pop" => { genres: -1 }) |
| 3793 | + expect(depeche_mode.reload.genres).to eq(["electronic"]) |
| 3794 | + end |
| 3795 | + |
| 3796 | + it "removes last element in array" do |
| 3797 | + context.update_all("$pop" => { genres: 1 }) |
| 3798 | + expect(depeche_mode.reload.genres).to eq(["pop"]) |
| 3799 | + end |
| 3800 | + end |
| 3801 | + |
| 3802 | + context "when operation is $pullAll" do |
| 3803 | + |
| 3804 | + before do |
| 3805 | + depeche_mode.update_attribute(:genres, ["pop", "electronic", "dance", "pop" ]) |
| 3806 | + new_order.update_attribute(:genres, ["electronic", "pop", "electronic", "dance"]) |
| 3807 | + context.update_all("$pullAll" => { genres: ["pop", "electronic"] }) |
| 3808 | + end |
| 3809 | + |
| 3810 | + it "updates the first matching document" do |
| 3811 | + expect(depeche_mode.reload.genres).to eq(["dance"]) |
| 3812 | + end |
| 3813 | + |
| 3814 | + it "updates the last matching document" do |
| 3815 | + expect(new_order.reload.genres).to eq(["dance"]) |
| 3816 | + end |
| 3817 | + end |
3749 | 3818 | end
|
3750 | 3819 |
|
3751 | 3820 | context 'when using aliased field names' do
|
|
0 commit comments