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

System.FormatException when trying to rename a field in the projection step of an aggregation pipeline

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

      I'm trying to using an aggregation projection stage to convert input documents to output documents with calculated properties. Using a C# expression with a definite type, e.g. 

      var projection = Builders<TaskEntity>.Projection
          .Expression(e => new TaskHeader
          {
              Id = e.Id,
              Name = e.Name,
              LinkCount = e.Links.Length
          });

      results in 

      System.FormatException: Element 'Links' does not match any field or property of class TaskHeader.

      whereas defining the projection step using a BsonDocument as below works as expected

      var projection = new BsonDocumentProjectionDefinition<TaskEntity, TaskHeader>(
          BsonDocument.Parse(@"{
            Name: 1,
            LinkCount: { $size: ""$Links"" }
          }"));

      Is there some way to build valid typed projections via C# expression tree when the output property names don't match the input property names? I thought this may have been fixed by the V3 Linq provider but that seems to give the same exception.

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            david.wolsey@gmail.com David Wolsey
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: