C# Driver: Projection vs. Object Style Casting

I did try to search the forums and documentation, so apologies if I missed it.

I am trying to determine if the driver would automatically project(mongo use of the word) an object automatically depending on what I ask the driver for from a C# object perspective or if I need to specifically project.

I am specifically asking in terms of how this would relate to network traffic.

Code below is not exact(writing it in the post) but the concept is there
Example:

public class Pet 
{
string Name;
int Age;
}
public class Dog : Pet
{
string Breed;
}

//here i am working with the collection of Pets and I am asking for a "Pet" object, which only has 2 properties. It does not have the breed property. If the object I am getting has the breed property is that property returned from Mongo and then ignored/removed at the driver level, or it is blocked from being returned, as it would with a project
var coll = DB.GetCollection<Pets>("Pets");
coll.Find<Pet>(x => x.Name == "Bob").FirstOrDefault();

VS

var simpleProjection = Builders<Pet>.Projection
    .Include(u => u.Name)
    .Include(u => u.Age);
var coll = DB.GetCollection<Pet>("Pets");
coll.Aggregate()
.Match(x => x.Name == "Bob")
.Project(simpleProjection )
.ToList()

I am not sure how to figure out or measure what data actually comes over the network(from mongo atlas to my local machine) and what data is just filtered out after the return vs. filtered out server-side

Primary Goal/Reason for Question: Attempting to reduce ingress and egress of the data coming out of Mongo as we don’t always need all properties.
We have been doing it via the C# casting method, but I have no idea how to measure the network traffic on this.

Hi @Mark_Mann

With C# driver you can inspect the content of “Command succeeded” event, by either subscribing to events and inspect the CommandSucceededEvent.Reply field or via logging and specifying { “LogLevel:MongoDB.Command”, “Debug” } component.

1 Like

Sorry it took so long to respond, I got side tracked but thank you. That is exactly what I needed.

I can confirm that using standard .NET casting with the C# driver does NOT change the payload size, which somewhat makes sense. It seems it retrieves the entire document and then casts/converts/serializes(whatever term) into the .NET object I told it to.

All finds were by ID to keep it simple.
Find as my object = 9067 bytes
Find as a smaller object with only 1 property = 9067 bytes
Find as my object + Project aggregation = 257 bytes