Summary
Driver Version: 2.21.0; No issue up to 2.20.0;
MongoDB Version: 6.0.10
When projecting a search result to the Id field of a nested document, the top-level Id field is returned.
See the following (simplified) example.
Expected: subDocumentId contains the value of the Id field of SubDocument.
Actual: subDocumentId contains the value of the Id field of Document.
Up to driver version 2.20.0, this works as expected.
public class BusinessLogic
{
public async Task<ObjectId> GetSubDocumentId()
{
var subDocumentId = await _dbContext.Documents.Find(d => d.Status == status)
.Project(d => d.PairedSwitchingDevice.Id)
.FirstOrDefaultAsync();
return subDocumentId;
}
}
public class Document
{
[BsonId]
public ObjectId Id \{ get; set; }
[BsonElement("Status")]
public string Status { get; set; }
[BsonElement("SubDocument")]
public SubDocument SubDocument { get; set; }
}
public class SubDocument
{
[BsonElement("Id")]
public ObjectId Id \{ get; set; }
}
|