|
3640 | 3640 | expect(new_order.reload.genres).to eq(["electronic"])
|
3641 | 3641 | end
|
3642 | 3642 | end
|
| 3643 | + |
| 3644 | + context "when operation is $pull" do |
| 3645 | + context "when pulling single element" do |
| 3646 | + |
| 3647 | + before do |
| 3648 | + depeche_mode.update_attribute(:genres, ["electronic", "pop"]) |
| 3649 | + new_order.update_attribute(:genres, ["electronic", "pop"]) |
| 3650 | + context.update_all("$pull" => { genres: "electronic" }) |
| 3651 | + end |
| 3652 | + |
| 3653 | + it "updates the first matching document" do |
| 3654 | + expect(depeche_mode.reload.genres).to eq(["pop"]) |
| 3655 | + end |
| 3656 | + |
| 3657 | + it "updates the last matching document" do |
| 3658 | + expect(new_order.reload.genres).to eq(["pop"]) |
| 3659 | + end |
| 3660 | + end |
| 3661 | + |
| 3662 | + context "when pulling based on condition" do |
| 3663 | + before do |
| 3664 | + depeche_mode.update_attribute(:genres, ["electronic", "pop", "dance"]) |
| 3665 | + new_order.update_attribute(:genres, ["electronic", "pop", "dance"]) |
| 3666 | + context.update_all("$pull" => { genres: { '$in' => ["electronic", "pop"] } }) |
| 3667 | + end |
| 3668 | + |
| 3669 | + it "updates the first matching document" do |
| 3670 | + expect(depeche_mode.reload.genres).to eq(["dance"]) |
| 3671 | + end |
| 3672 | + |
| 3673 | + it "updates the last matching document" do |
| 3674 | + expect(new_order.reload.genres).to eq(["dance"]) |
| 3675 | + end |
| 3676 | + end |
| 3677 | + end |
| 3678 | + |
| 3679 | + context "when operation is $pop" do |
| 3680 | + |
| 3681 | + before do |
| 3682 | + depeche_mode.update_attribute(:genres, ["pop", "electronic"]) |
| 3683 | + end |
| 3684 | + |
| 3685 | + it "removes first element in array" do |
| 3686 | + context.update_all("$pop" => { genres: -1 }) |
| 3687 | + expect(depeche_mode.reload.genres).to eq(["electronic"]) |
| 3688 | + end |
| 3689 | + |
| 3690 | + it "removes last element in array" do |
| 3691 | + context.update_all("$pop" => { genres: 1 }) |
| 3692 | + expect(depeche_mode.reload.genres).to eq(["pop"]) |
| 3693 | + end |
| 3694 | + end |
| 3695 | + |
| 3696 | + context "when operation is $pullAll" do |
| 3697 | + |
| 3698 | + before do |
| 3699 | + depeche_mode.update_attribute(:genres, ["pop", "electronic", "dance", "pop" ]) |
| 3700 | + new_order.update_attribute(:genres, ["electronic", "pop", "electronic", "dance"]) |
| 3701 | + context.update_all("$pullAll" => { genres: ["pop", "electronic"] }) |
| 3702 | + end |
| 3703 | + |
| 3704 | + it "updates the first matching document" do |
| 3705 | + expect(depeche_mode.reload.genres).to eq(["dance"]) |
| 3706 | + end |
| 3707 | + |
| 3708 | + it "updates the last matching document" do |
| 3709 | + expect(new_order.reload.genres).to eq(["dance"]) |
| 3710 | + end |
| 3711 | + end |
3643 | 3712 | end
|
3644 | 3713 |
|
3645 | 3714 | context 'when using aliased field names' do
|
|
0 commit comments