3

I've registered a class to pull addresses out of the a restaurants collection:

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");
});

The Mongo document looks 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"
}

When I get the document, the nested elements are all null though:

[Test]
public void DeserializeToAddress()
{
    var collection = _database.GetCollection<RestaurantAddress>("grades");

    var address = collection.Find(a => a.Id == new ObjectId("56bb82621ff72e0d9ba267cb")).Limit(1).Single();

    Assert.That(address.Id, Is.Not.Null);
    Assert.That(address.Building, Is.Not.Null);
    Assert.That(address.Street, Is.Not.Null);
}

How should I be referencing nested elements?

The class to serialize into is:

internal class RestaurantAddress
{
    public ObjectId Id { get; set; }
    public string Building { get; set; }
    public string Street { get; set; }
    public string ZipCode { get; set; }
    public Point CoordsPoint { get; set; }
}

Currenlty, I don't have anything for hyrating the CoordsPoint object. That's for a later excercise.

1
  • Can you post your class definition? Commented Feb 12, 2016 at 13:24

1 Answer 1

1

We currently don't support pulling nested elements out into a parent document. This would be a feature request. You can file that at jira.mongodb.org under the CSHARP project. Alternatively, you can write a custom serializer to handle this particular scenario if you'd like.

Craig

Sign up to request clarification or add additional context in comments.

2 Comments

Cheers, I'll add a request. Do I click 'create issue' or is there another link?
How do you mean you support pulling nested documents? Am I not able to deserialize a nested object especially member of nested arrays to a c# Object?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.