[CSHARP-1571] Map to nested elements Created: 12/Feb/16  Updated: 19/Mar/19  Resolved: 19/Mar/19

Status: Closed
Project: C# Driver
Component/s: BSON, Feature Request, Serialization
Affects Version/s: 2.2.3
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: David Banks Assignee: Robert Stam
Resolution: Done Votes: 1
Labels: BsonClassMap, Feature, Mapping, Serialization, feature
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Not environment specific



 Description   

As per question on SO: http://stackoverflow.com/questions/35362675/mongodb-map-class-from-nested-elements-in-c-sharp

When creating a class map in the BsonClassMap class, I would like to be able to reference nested elements.

e.g.

BsonClassMap.RegisterClassMap<RestaurantAddress>(map =>
{
    map.MapMember(c => c.Id).SetElementName(("_id"));
    map.MapMember(c => c.Building).SetElementName("address.building");
    map.MapMember(c => c.Street).SetElementName("address.street");
    map.MapMember(c => c.ZipCode).SetElementName("address.zipcode");
});

For a document like:

{
    "_id" : ObjectId("56bb82621ff72e0d9ba267cb"),
    "address" : {
        "building" : "6409",
        "coord" : [ 
            -74.005289, 
            40.628886
        ],
        "street" : "11 Avenue",
        "zipcode" : "11219"
    },
    "borough" : "Brooklyn",
    "cuisine" : "American ",
    "grades" : [ 
        {
            "date" : ISODate("2014-07-18T00:00:00.000Z"),
            "grade" : "A",
            "score" : 12
        }, 
        {
            "date" : ISODate("2013-07-30T00:00:00.000Z"),
            "grade" : "A",
            "score" : 12
        }
    ],
    "name" : "Regina Caterers",
    "restaurant_id" : "40356649"
}



 Comments   
Comment by Robert Stam [ 19/Mar/19 ]

The BsonClassMapSerializer can only be used when each nested document corresponds to a C# class.

If you want your C# class structure to diverge from your document structure you will have to write a custom serializer. We have no plans to support anything like this in BsonClassMapSerializer itself.

Generated at Wed Feb 07 21:40:00 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.