(bugfix): upsert and update many logics

This commit is contained in:
Maksym Sadovnychyy 2024-10-06 00:48:33 +02:00
parent e353845fb4
commit 64f6f70e65
2 changed files with 42 additions and 17 deletions

View File

@ -89,14 +89,37 @@ namespace MaksIT.MongoDB.Linq.Abstractions {
Expression<Func<TDtoDocument, bool>> predicate, Expression<Func<TDtoDocument, bool>> predicate,
IClientSessionHandle? session) { IClientSessionHandle? session) {
try { try {
foreach (var document in documents) { // Step 1: Find the documents that already exist based on the predicate
if (session != null) List<TDtoDocument> existingDocuments;
await Collection.ReplaceOneAsync(session, predicate, document); if (session != null) {
else existingDocuments = await Collection.Find(session, predicate).ToListAsync();
await Collection.ReplaceOneAsync(predicate, document); }
else {
existingDocuments = await Collection.Find(predicate).ToListAsync();
} }
var updatedIds = documents.Select(doc => doc.Id).ToList(); // Step 2: Get the existing document IDs
var existingIds = existingDocuments.Select(doc => doc.Id).ToHashSet();
// Step 3: Filter the documents to update only those that exist in the collection
var documentsToUpdate = documents.Where(doc => existingIds.Contains(doc.Id)).ToList();
// Step 4: Update each of the existing documents
foreach (var document in documentsToUpdate) {
// Handling nullable Id by checking both document.Id and x.Id for null
var documentPredicate = (Expression<Func<TDtoDocument, bool>>)(x =>
(x.Id == null && document.Id == null) ||
(x.Id != null && x.Id.Equals(document.Id)));
if (session != null) {
await Collection.ReplaceOneAsync(session, documentPredicate, document);
}
else {
await Collection.ReplaceOneAsync(documentPredicate, document);
}
}
var updatedIds = documentsToUpdate.Select(doc => doc.Id).ToList();
return Result<List<TDtoKey>?>.Ok(updatedIds); return Result<List<TDtoKey>?>.Ok(updatedIds);
} }
catch (Exception ex) { catch (Exception ex) {
@ -137,16 +160,18 @@ namespace MaksIT.MongoDB.Linq.Abstractions {
Expression<Func<TDtoDocument, bool>> predicate, Expression<Func<TDtoDocument, bool>> predicate,
IClientSessionHandle? session) { IClientSessionHandle? session) {
try { try {
foreach (var document in documents) { // Deletion
if (session != null) { if (session != null)
await Collection.DeleteOneAsync(session, predicate); await Collection.DeleteManyAsync(session, predicate);
await Collection.InsertOneAsync(session, document); else
} await Collection.DeleteManyAsync(predicate);
else {
await Collection.DeleteOneAsync(predicate); // Creation
await Collection.InsertOneAsync(document); if (session != null)
} await Collection.InsertManyAsync(session, documents);
} else
await Collection.InsertManyAsync(documents);
var upsertedIds = documents.Select(doc => doc.Id).ToList(); var upsertedIds = documents.Select(doc => doc.Id).ToList();
return Result<List<TDtoKey>?>.Ok(upsertedIds); return Result<List<TDtoKey>?>.Ok(upsertedIds);

View File

@ -8,7 +8,7 @@
<!-- NuGet package metadata --> <!-- NuGet package metadata -->
<PackageId>MaksIT.MongoDB.Linq</PackageId> <PackageId>MaksIT.MongoDB.Linq</PackageId>
<Version>1.0.4</Version> <Version>1.0.5</Version>
<Authors>Maksym Sadovnychyy</Authors> <Authors>Maksym Sadovnychyy</Authors>
<Company>MAKS-IT</Company> <Company>MAKS-IT</Company>
<Product>MaksIT.MongoDB.Linq</Product> <Product>MaksIT.MongoDB.Linq</Product>