Reputation: 28560
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.
Upvotes: 3
Views: 3056
Reputation: 12624
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
Upvotes: 1