Suppose I have the following data in my MongoDB:
{
"_id": {
"$oid": "62baf9a2851424fdb8c226f8"
},
"Name": "Team A",
"TeamData": {
"Players": [
{
"FirstName": "First Name A",
"LastName": "Last Name A"
},
{
"FirstName": "First Name B",
"LastName": "Last Name B"
}
]
}
}
I only want to query LastName
. FirstName
should be null
.
The query projection looks like this, using dot notation:
{
"TeamData.Players.LastName": 1
}
How can I do this using the C# driver's LINQ support? I was able to come close with the code below, but not quite.
public IEnumerable<TeamDto> GetTeam()
{
return _collection.Find(filter).Project(x => new TeamDto
{
Id = x.Id,
TeamData = new TeamDataDto
{
Players = x.TeamData.Select(y => new PlayerDto
{
LastName = y.LastName
})
}
});
}
The result is: Players array is populated, but with null
values. I also tried my hand at Include
and Exclude
inside .Project()
, also to no avail. Can anyone shed a light?
Edit: My question is actually very similar to this one, but using LINQ instead. I know there's a way to put dot notation in .Project
, but that's also not want I want. Thanks.