3

I am running a map reduce job that dumps the resulting data into a collection, the elements in the "products" collection look like this (the structure is generated by Mongo and I'm not aware if it can be controlled):

{
    "_id" : { "ProductId" : "1:000001", "ProductTitle" : "Some product with ID 1:000001" }, 
    "value" : { "TotalImpressions" : 3, "TotalClicks" : 40 } 
}

Ideally, I want to map each entry to the following flat object:

public class Product 
{
    public string ProductId { get; set; }
    public string ProductTitle { get; set; }
    public int TotalImpressions { get; set; }
}

This obviously doesn't work as the serializer looks for properties "id" and "value" at the root level, which don't exist on that class. The workaround I have in place is to model the object as they appear, e.g.:

public class ProductRow
{
    /* implementation of these objects excluded, but they just reflect the json objects */
    public ProductIdentifier Id { get; set; }
    public Product value { get; set; }
}

Which maps fine, however it's a little verbose and I'd rather avoid having all those extra objects.

Is it possible to configure the BSON deserializer to support this mapping? I've had a look through the documentation, but haven't seen any obvious solution.

NB: I am restricted by working environment to .NET 3.5, so please bear that in mind when considering an answer.

shA.t
  • 16,580
  • 5
  • 54
  • 111
gooid
  • 841
  • 8
  • 18
  • Can't you just map the untyped results to Product instances yourself, after the query? Why do you want the BSON deserializer to do this for you? It's not strictly its job... – Avish Jun 27 '11 at 13:28
  • Yes, I could do it manually, which is a valid point - so thanks. But I disagree that this is not a function of the deserializer, it is exactly a problem in it's domain, albeit one which I accept is not likely to be implemented due to the fact is arguably an edge case, hence the question. – gooid Jun 28 '11 at 08:25

1 Answers1

4

You can easy do deserialization yourself(as @Avish suggested), so here is complete example for your case:

var mongoServer = MongoServer.Create("mongodb://localhost:27020");
var database = mongoServer.GetDatabase("StackoverflowExamples");
var products = database.GetCollection("products");

var result = new Product();

var item = products.FindOne();
var id = item["_id"].ToBsonDocument();
var value = item["value"].ToBsonDocument();

result.ProductId = id["ProductId"].AsString;
result.ProductTitle = id["ProductTitle"].AsString;
result.TotalImpressions = value["TotalImpressions"].AsInt32;
Andrew Orsich
  • 52,935
  • 16
  • 139
  • 134