From 37e64d7d24cb24e3ac549b66c8d6ccd432e376d4 Mon Sep 17 00:00:00 2001 From: Maksym Sadovnychyy Date: Fri, 25 Oct 2024 21:48:44 +0200 Subject: [PATCH] (feature): allow empty collections for insert, update and upsert many --- .../BaseCollectionDataProviderBase.cs | 15 +++++++++++++++ .../MaksIT.MongoDB.Linq.csproj | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/MaksIT.MongoDB.Linq/Abstractions/BaseCollectionDataProviderBase.cs b/src/MaksIT.MongoDB.Linq/Abstractions/BaseCollectionDataProviderBase.cs index 98df00b..74d91ad 100644 --- a/src/MaksIT.MongoDB.Linq/Abstractions/BaseCollectionDataProviderBase.cs +++ b/src/MaksIT.MongoDB.Linq/Abstractions/BaseCollectionDataProviderBase.cs @@ -46,6 +46,11 @@ namespace MaksIT.MongoDB.Linq.Abstractions { #region InsertMany protected virtual async Task?>> InsertManyAsync(List documents, IClientSessionHandle? session) { try { + // Check if the documents list is empty + if (documents.Count == 0) { + return Result?>.Ok(new List()); + } + if (session != null) await Collection.InsertManyAsync(session, documents); else @@ -90,6 +95,11 @@ namespace MaksIT.MongoDB.Linq.Abstractions { Expression> predicate, IClientSessionHandle? session) { try { + // Check if the documents list is empty + if (documents.Count == 0) { + return Result?>.Ok(new List()); + } + // Step 1: Find the documents that already exist based on the predicate List existingDocuments; if (session != null) { @@ -161,6 +171,11 @@ namespace MaksIT.MongoDB.Linq.Abstractions { Expression> predicate, IClientSessionHandle? session) { try { + // Check if the documents list is empty + if (documents.Count == 0) { + return Result?>.Ok(new List()); + } + // Deletion if (session != null) await Collection.DeleteManyAsync(session, predicate); diff --git a/src/MaksIT.MongoDB.Linq/MaksIT.MongoDB.Linq.csproj b/src/MaksIT.MongoDB.Linq/MaksIT.MongoDB.Linq.csproj index 88385d6..19bda32 100644 --- a/src/MaksIT.MongoDB.Linq/MaksIT.MongoDB.Linq.csproj +++ b/src/MaksIT.MongoDB.Linq/MaksIT.MongoDB.Linq.csproj @@ -8,7 +8,7 @@ MaksIT.MongoDB.Linq - 1.0.6 + 1.0.7 Maksym Sadovnychyy MAKS-IT MaksIT.MongoDB.Linq