I am encountering an issue with the latest version (2.19.0) of the MongoDB .NET driver. When performing an upsert operation using the ReplaceOneAsync method with an Expression<Func<T, bool>> filter that always returns true, the following error occurs:
MongoDB.Driver.MongoWriteException: A write operation resulted in an error. WriteError:
{ Category : "Uncategorized", Code : 224, Message : "$expr is not allowed in the query predicate for an upsert" }.
This error was not present in the previous version (2.18.0) of the MongoDB .NET driver.
Steps to Reproduce:
> Use the latest version (2.19.0) of the MongoDB .NET driver.
> Perform an upsert operation using the ReplaceOneAsync method with an Expression<Func<T, bool>> filter that always returns true.
> Observe the error message mentioned above.
Expected Result:
The upsert operation should complete without any error, as it did in previous versions of the MongoDB .NET driver.
Actual Result:
The upsert operation fails with the error message "$expr is not allowed in the query predicate for an upsert" in MongoDB .NET driver version 2.19.0.
Environment:
MongoDB .NET driver version: 2.19.0
Platform: Windows
.NET runtime version: .NET 6
Code snippet:
// Micro-service specific code
public async Task SyncModelNameCollection(ModelName data)
// genric library code
public async Task<long?> Upsert<T>(T data, string collectionName, Expression<Func<T, bool>> filter)
{
var options = new ReplaceOptions
;
var result = await _db.GetCollection<T>(collectionName).ReplaceOneAsync(filter, data, options);
if (result.IsModifiedCountAvailable)
return null;
}