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

Projection ObjectId using C# driver

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.9.1
    • Component/s: Builders
    • None
    • Fully Compatible

      I have the following models:

      public class EntityInfo
      {
          [BsonId, BsonRepresentation(BsonType.ObjectId)]
          public string Id { get; set; }
      
          public string Name { get; set; }
      
          IEnumerable<string> Towns { get; set; }
      }
      
      public class EntityWithTowns
      {
          [BsonId, BsonRepresentation(BsonType.ObjectId)]
          public string Id { get; set; }
      
          public string Name { get; set; }
          
          IEnumerable<Towns> Towns { get; set; }
      }
      

      When i am trying to add projection stage to aggregation pipeline:

      .Project(ewt => new EntityInfo
      {
          Id = ewt.Id,
          Name = ewt.Name,
          Towns = ewt.Towns.Select(t => t.Id)
      })
      

      Driver generates the following query text:

      { \"$project\" : { \"Id\" : \"$_id\", \"Name\" : \"$Name\", \"Towns\" : \"$Towns._id\", \"_id\" : 0 } }
      

      What breaks the following stages, because it generates "Id" field instead "_id" and i should write something like this:

      .Project<EntityInfo>(new BsonDocument(new List<KeyValuePair<string, object>>
      {
          new KeyValuePair<string, object>("_id", "$_id"),
          new KeyValuePair<string, object>("Name", "$Name"),
          new KeyValuePair<string, object>("Towns", "$Towns._id")
      }))
      

      Here is link to closed issue, what describes same problem:
      https://jira.mongodb.org/browse/CSHARP-1768

            Assignee:
            Unassigned Unassigned
            Reporter:
            sokolovdv@katren.ru Dmitry Sokolov
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: