Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-4622

Error with MongoDB .NET driver 2.19.0: $expr not allowed in query predicate for upsert

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Blocker - P1 Blocker - P1
    • 2.19.2
    • Affects Version/s: None
    • Component/s: LINQ3
    • Labels:
    • Fully Compatible
    • Not Needed
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      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)

      { var updatedCount = await _db.Upsert(data, ModelName.DBCollectionName, (d) => true); }

      // genric library code
      public async Task<long?> Upsert<T>(T data, string collectionName, Expression<Func<T, bool>> filter)
      {
      var options = new ReplaceOptions

      { IsUpsert = true }

      ;
      var result = await _db.GetCollection<T>(collectionName).ReplaceOneAsync(filter, data, options);
      if (result.IsModifiedCountAvailable)

      { return result.ModifiedCount; }

      return null;
      }

            Assignee:
            oleksandr.poliakov@mongodb.com Oleksandr Poliakov
            Reporter:
            hardikkumar.shah@nationwide.co.uk Hardik Shah
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: