[CSHARP-2787] Projection ObjectId using C# driver Created: 11/Oct/19  Updated: 31/Mar/22

Status: Backlog
Project: C# Driver
Component/s: Builders
Affects Version/s: 2.9.1
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Dmitry Sokolov Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: 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


Generated at Wed Feb 07 21:43:32 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.