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

Projection ObjectId using C# driver

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Major - P3 Major - P3
    • None
    • 2.9.1
    • Builders
    • None
    • Fully Compatible

    Description

      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

      Attachments

        Activity

          People

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

            Dates

              Created:
              Updated: