From e7a50264384ce53d36285051fccffbd79bdaff6b Mon Sep 17 00:00:00 2001
From: "Phaidra Devel (phaidra2)" <phaidra.devel@univie.ac.at>
Date: Fri, 3 Mar 2023 17:03:51 +0100
Subject: [PATCH] added patched_remove to emulate the old "remove" method using
 "delete_one" and "delete_many"

---
 modules/util/Util/MongoDB.pm | 27 ++++++++++++++++++++++++++-
 1 file changed, 26 insertions(+), 1 deletion(-)

diff --git a/modules/util/Util/MongoDB.pm b/modules/util/Util/MongoDB.pm
index 5077f3b..16425b9 100644
--- a/modules/util/Util/MongoDB.pm
+++ b/modules/util/Util/MongoDB.pm
@@ -354,9 +354,10 @@ sub patch
 
   if (defined (&MongoDB::Collection::insert_one))
   {
-    print STDERR "MongoDB::Collection::insert undefined, patching...\n";
+    # print STDERR "MongoDB::Collection::insert undefined, patching...\n";
     *MongoDB::Collection::insert= *MongoDB::Collection::insert_one;
     *MongoDB::Collection::delete= *MongoDB::Collection::delete_one;
+    *MongoDB::Collection::remove= *patched_remove;
     *MongoDB::Collection::update= *patched_update;
     $patched= 1;
   }
@@ -368,6 +369,30 @@ sub patch
   $patched;
 }
 
+sub patched_remove
+{
+  my ($col, $filter, $opt)= @_;
+  $filter= {} unless (defined ($filter));
+  # print __LINE__, " patched_remove: filter: ", Dumper($filter);
+  # print __LINE__, " patched_remove: opt: ", Dumper($opt);
+  # print __LINE__, " patched_remove: caller: ", join(' ', caller()), "\n";
+
+  my $just_one= (defined($opt) && exists ($opt->{just_one}) && $opt->{just_one}) ? 1 : 0;
+  # delete($opt->{just_one});
+
+  my $res;
+  if ($just_one)
+  {
+    $res= MongoDB::Collection::delete_one($col, $filter, $opt);
+  }
+  else
+  {
+    $res= MongoDB::Collection::delete_many($col, $filter, $opt);
+  }
+
+  $res;
+}
+
 sub patched_update
 {
   my ($col, $filter, $upd, $opt)= @_;
-- 
GitLab