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

ExpressionNotSupportedException with projection expression

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: 2.20.0
    • Component/s: None
    • Labels:
      None
    • 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?

      Summary

      After upgrading the MongoDB C# driver from 2.15.0 to 2.20.0, I am getting ExpressionNotSupportedException for one of my queries. I am running MongoDB 5.0.18.

      How to Reproduce

      I have the following object:

       

      public class UnlockableModel
      {
          [BsonId]
          public Guid profileId { get; set; }
          public Dictionary<String, List<String>> entries { get; set; }
      }

      The following query will fail with ExpressionNotSupportedException:

      List<string> categories = new List<string> {"Test"};
      
      var filter = Builders<UnlockableModel>.Filter.Eq(model => model.profileId, profileId);
      var options = new FindOptions<UnlockableModel>
      {
          Projection = Builders<UnlockableModel>.Projection.Expression(p => new UnlockableModel
          {
              profileId = p.profileId,
              entries = (p.entries != null) ? new(p.entries.Where(kvp => categories.Contains(kvp.Key))) : new()
          })
      };
      
      var result = await _unlockableCollection.FindAsync(filter, options);

      Downgrading the connection to LinqProvider.V2 fixes the issue:

      var connectionString = "mongodb://localhost";
      MongoClientSettings settings = MongoClientSettings.FromConnectionString(connectionString);
      settings.LinqProvider = LinqProvider.V2;

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            loris.blanchon@ubisoft.com Loris Blanchon
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: