From a427c6f20189e538411798718e8dba0731d92044 Mon Sep 17 00:00:00 2001 From: Dary Cabrera Date: Wed, 11 Sep 2024 07:11:51 -0600 Subject: [PATCH] Update mongo repsert and replace calls Replace mongo driver save and replace calls with updateMany to restore correct behavior in MongoDB >=6.0 --- persistent-mongoDB/Database/Persist/MongoDB.hs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/persistent-mongoDB/Database/Persist/MongoDB.hs b/persistent-mongoDB/Database/Persist/MongoDB.hs index 71cd23557..679ce540a 100644 --- a/persistent-mongoDB/Database/Persist/MongoDB.hs +++ b/persistent-mongoDB/Database/Persist/MongoDB.hs @@ -371,10 +371,6 @@ queryByKey :: (PersistEntity record, PersistEntityBackend record ~ DB.MongoConte => Key record -> DB.Query queryByKey k = (DB.select (keyToMongoDoc k) (collectionNameFromKey k)) {DB.project = projectionFromKey k} -selectByKey :: (PersistEntity record, PersistEntityBackend record ~ DB.MongoContext) - => Key record -> DB.Selection -selectByKey k = DB.select (keyToMongoDoc k) (collectionNameFromKey k) - updatesToDoc :: (PersistEntity record, PersistEntityBackend record ~ DB.MongoContext) => [Update record] -> DB.Document updatesToDoc upds = map updateToMongoField upds @@ -554,12 +550,16 @@ instance PersistStoreWrite DB.MongoContext where insertKey k record = DB.insert_ (collectionName record) $ entityToInsertDoc (Entity k record) - repsert k record = DB.save (collectionName record) $ - documentFromEntity (Entity k record) + repsert k record = + void $ DB.updateMany + (collectionName record) + [(keyToMongoDoc k, documentFromEntity (Entity k record), [DB.Upsert])] - replace k record = do - DB.replace (selectByKey k) (recordToDocument record) - return () + replace k record = + -- replace a single matching document + void $ DB.updateMany + (collectionNameFromKey k) + [(keyToMongoDoc k, recordToDocument record, [])] delete k = void $ DB.deleteMany