(bugfix): workaround to replace native mongo upsert, as it's not supporting expressions
This commit is contained in:
parent
d341eb994e
commit
cdaf05e931
@ -93,22 +93,15 @@ namespace MaksIT.MongoDB.Linq.Abstractions {
|
|||||||
Expression<Func<TDtoDocument, bool>> predicate,
|
Expression<Func<TDtoDocument, bool>> predicate,
|
||||||
IClientSessionHandle? session) {
|
IClientSessionHandle? session) {
|
||||||
try {
|
try {
|
||||||
var tasks = new List<Task<ReplaceOneResult>>();
|
|
||||||
|
|
||||||
foreach (var document in documents) {
|
foreach (var document in documents) {
|
||||||
var filter = Builders<TDtoDocument>.Filter.Where(predicate);
|
|
||||||
var updateOptions = new ReplaceOptions { IsUpsert = false };
|
|
||||||
|
|
||||||
if (session != null)
|
if (session != null)
|
||||||
tasks.Add(Collection.ReplaceOneAsync(session, filter, document, updateOptions));
|
await Collection.ReplaceOneAsync(session, predicate, document);
|
||||||
else
|
else
|
||||||
tasks.Add(Collection.ReplaceOneAsync(filter, document, updateOptions));
|
await Collection.ReplaceOneAsync(predicate, document);
|
||||||
}
|
}
|
||||||
|
|
||||||
await Task.WhenAll(tasks);
|
var updatedIds = documents.Select(doc => doc.Id).ToList();
|
||||||
|
return Result<List<TDtoKey>?>.Ok(updatedIds);
|
||||||
var upsertedIds = documents.Select(doc => doc.Id).ToList();
|
|
||||||
return Result<List<TDtoKey>?>.Ok(upsertedIds);
|
|
||||||
}
|
}
|
||||||
catch (Exception ex) {
|
catch (Exception ex) {
|
||||||
Logger.LogError(ex, _errorMessage);
|
Logger.LogError(ex, _errorMessage);
|
||||||
@ -124,12 +117,14 @@ namespace MaksIT.MongoDB.Linq.Abstractions {
|
|||||||
IClientSessionHandle? session
|
IClientSessionHandle? session
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
var updateOptions = new ReplaceOptions { IsUpsert = true };
|
if (session != null) {
|
||||||
|
await Collection.DeleteOneAsync(session, predicate);
|
||||||
if (session != null)
|
await Collection.InsertOneAsync(session, document);
|
||||||
await Collection.ReplaceOneAsync(session, predicate, document, updateOptions);
|
}
|
||||||
else
|
else {
|
||||||
await Collection.ReplaceOneAsync(predicate, document, updateOptions);
|
await Collection.DeleteOneAsync(predicate);
|
||||||
|
await Collection.InsertOneAsync(document);
|
||||||
|
}
|
||||||
|
|
||||||
return Result<TDtoKey?>.Ok(document.Id);
|
return Result<TDtoKey?>.Ok(document.Id);
|
||||||
}
|
}
|
||||||
@ -146,20 +141,17 @@ namespace MaksIT.MongoDB.Linq.Abstractions {
|
|||||||
Expression<Func<TDtoDocument, bool>> predicate,
|
Expression<Func<TDtoDocument, bool>> predicate,
|
||||||
IClientSessionHandle? session) {
|
IClientSessionHandle? session) {
|
||||||
try {
|
try {
|
||||||
var tasks = new List<Task<ReplaceOneResult>>();
|
|
||||||
|
|
||||||
foreach (var document in documents) {
|
foreach (var document in documents) {
|
||||||
var filter = Builders<TDtoDocument>.Filter.Where(predicate);
|
if (session != null) {
|
||||||
var updateOptions = new ReplaceOptions { IsUpsert = true };
|
await Collection.DeleteOneAsync(session, predicate);
|
||||||
|
await Collection.InsertOneAsync(session, document);
|
||||||
if (session != null)
|
}
|
||||||
tasks.Add(Collection.ReplaceOneAsync(session, filter, document, updateOptions));
|
else {
|
||||||
else
|
await Collection.DeleteOneAsync(predicate);
|
||||||
tasks.Add(Collection.ReplaceOneAsync(filter, document, updateOptions));
|
await Collection.InsertOneAsync(document);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await Task.WhenAll(tasks);
|
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
<!-- NuGet package metadata -->
|
<!-- NuGet package metadata -->
|
||||||
<PackageId>MaksIT.MongoDB.Linq</PackageId>
|
<PackageId>MaksIT.MongoDB.Linq</PackageId>
|
||||||
<Version>1.0.0</Version>
|
<Version>1.0.1</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>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user