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

Projection where one field name starts with another field name fails

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: Major - P3 Major - P3
    • 2.9.0
    • 2.8.0
    • Linq
    • None

    Description

      If a POCO has two fields where one name starts with the others name, any projection that uses these names will fail to render.

      Repro:

          public class Person
          {
              public string FirstName { get; set; }
              public string FirstNamePreferred { get; set; }
              public string LastName { get; set; }
          }
       
          class Program
          {
              static void Main(string[] args)
              {
                  var m = new MongoClient("mongodb://noused");
                  var coll = m.GetDatabase("test").GetCollection<Person>("foo");
       
                  var queryDefn = Builders<Person>.Filter.Eq(x => x.FirstName, "John");
                  var serializer = BsonSerializer.SerializerRegistry.GetSerializer<Person>();
                  // WORKING
                  var query = coll.Find(queryDefn).Project(x => new Person { FirstName = x.FirstName, LastName = x.LastName });
                  var render = query.Options.Projection.Render(serializer, BsonSerializer.SerializerRegistry).Document;
                  if (render == null || render.ToString() != "{ \"FirstName\" : 1, \"LastName\" : 1, \"_id\" : 0 }")
                      throw new Exception("Projection does not match");
                  // NOT WORKING
                  query = coll.Find(queryDefn).Project(x => new Person { FirstName = x.FirstName, FirstNamePreferred = x.FirstNamePreferred });
                  render = query.Options.Projection.Render(serializer, BsonSerializer.SerializerRegistry).Document;
                  if (render == null || render.ToString() != "{ \"FirstName\" : 1, \"FirstNamePreferred\" : 1, \"_id\" : 0 }")
                      throw new Exception("Projection does not match");
              }
          }
      

      The issue appears to arise from this check in FindProjectionTranslator.

      Attachments

        Activity

          People

            dmitry.lukyanov@mongodb.com Dmitry Lukyanov (Inactive)
            john.murphy@mongodb.com John Murphy
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: