Mongo .Net Driver ignores all Id values with Project's PipelineStageDefinitionBuilder facet

When using the PipelineStageDefinitionBuilder to create projection stages for an aggregation pipeline, the Id values in the dataset appear to be ignored. This is observed when using the Mongo .Net driver 2.8 in a .Net Core app. The same projection works when using IAggregateFluent syntax on Aggregate(), however within a facet it fails to bind any Id values.

Below is a simplified example of the code:

public class DatabaseModel
{
    public Guid Id { get; set; }
    public string Type { get; set; }
}

public class ProjectionClass
{
   public Guid Id { get; set; }
   public string Type { get; set; }
}

var typeFilter = Builders<DatabaseModel>.Filter.Eq(x => x.Type, "Full");

var aggregationPipeline = new EmptyPipelineDefinition<DatabaseModel>()
    .AppendStage(PipelineStageDefinitionBuilder.Match(typeFilter))
    .AppendStage(PipelineStageDefinitionBuilder.Project<DatabaseModel, ProjectionClass>(x => new ProjectionClass
    {
        Id = x.Id,
        Type = x.Type,
    }));

var normalAggregationResult = await db.Aggregate(aggregationPipeline).ToListAsync();

var databaseModelsFacet = AggregateFacet.Create("DatabaseModels", aggregationPipeline);
var faucetResult = db.Aggregate().Facet(databaseModelsFacet).SingleOrDefault().Facets;

var projectionModels = faucetResult.
    Single(x => x.Name == "DatabaseModels")
    .Output<ProjectionClass>();

The resulting Mongo query:

{[{ 
    "$match" : { "Type" : "Full" } }, 
  { "$project" : { "Id" : "$_id", "Type" : "$Type", "_id" : 0 } 
}]}

Is there any way to be able to run a projection using the pipeline builders with a facet while not ignoring the Id?

Yes, you can use BsonElement attribute to map the Id property to _id in the projection.

Here’s an updated version of the ProjectionClass:

public class ProjectionClass
{
   [BsonElement("_id")]
   public Guid Id { get; set; }
   public string Type { get; set; }
}

With this attribute, the Id property will be mapped to _id in the projection and the resulting query will be:

{[{ 
    "$match" : { "Type" : "Full" } }, 
  { "$project" : { "_id" : "$_id", "Type" : "$Type", "_id" : 0 } 
}]}

Note that the _id property is explicitly set to "$_id" to ensure that the Id property is mapped to _id.