Deserialized in .NET a sub document to it Class/Obj type

Hello Forum, I am struggling with what I thought would be easy. I am still sure it is easy, I just dont know how to do it.

I am nesting documents inside a main document like this

{
    "_id": {
        "$oid": "6138c7fe8c3877e834b82072"
    },
    "ServicePages": {
        "AboutUs": "{\"TestData\":\"Hello From MongoDb\"}"
    }
}

I need to be able to query this in .NET c# or vb just to return the AboutUs Obj deserialized to the AboutUsDto Class.

I tryed with AsQuerable, but to do this I had to duplicate the Id as a property in the Services Pages Object. I need to study more mongoDb, this should be easy.

    Public Class AboutUsServiceDto

        Public Property TestData As String

    End Class
Public Class CommonContainer

    <BsonId>
    <BsonElement("_Id", Order:=1)>
    <BsonRepresentation(BsonType.ObjectId)>
    Public Property Id As ObjectId

    Public Property AboutUs As AboutUsServiceDto

End Class

If possible I dont really want to duple the PK/_Id in the CommonContainer Object, just to have a structured Data Dto or even an array.

Any pointers/help/code would be very helpful, I will have about 16 small nested documents in the main document once I get this code working.

I am trying to do this in .NET

Kind Regards
Graham Mattingley

Hi, @Graham_Mattingley,

This is definitely possible with the MongoDB .NET/C# Driver. You can define your collections and query them in terms of your entities (aka business objects), but then project into view (or DTO) objects quite easily.

using MongoDB.Bson;
using MongoDB.Driver;

var client = new MongoClient();
var db = client.GetDatabase("test");
var coll = db.GetCollection<MainDocument>("nested");

var query = coll.AsQueryable()
                .Where(x => x.Id == new ObjectId("6138c7fe8c3877e834b82072"))
                .Select(x => new AboutUsServiceDto { TestData = x.ServicePages.AboutUs.TestData });

Console.WriteLine(query);

var result = query.FirstOrDefault();
Console.WriteLine(result);

record MainDocument(ObjectId Id, ServicePages ServicePages);
record ServicePages(Page AboutUs);
record Page(string TestData);

class AboutUsServiceDto
{
    public string TestData { get; set; }

    public override string ToString() => $"{TestData}";
}

I defined the entities as C#9 records to save some typing, but the same code works with classes as well. The output of this program is:

aggregate([{ "$match" : { "_id" : ObjectId("6138c7fe8c3877e834b82072") } }, { "$project" : { "TestData" : "$ServicePages.AboutUs.TestData", "_id" : 0 } }])
Hello from MongoDB

Note how the LINQ Select resulted in the necessary data being projected into the output.

The .NET/C# Driver provides a lot of flexibility to map database structures into .NET objects. In most/all cases you shouldn’t need to duplicate your PK/_id into child documents stored within the parent document.

Sincerely,
James

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.