2

Let's say this is a collection of 2 Bson documents

{
    "_id": "...",
    "name": "Test1",
    "sub": {
        "street": "134 Fake Street",
        "city": "NoWhere"
    }
},
{
    "_id": "...",
    "name": "Test2",
    "sub": {
        "height": "10",
        "width": "20",
        "sub2": {
            "type": "something"
        }
    }
}

where the first level is a structured class but sub-levels can be completely unstructured and can have further nested documents several levels deep.

How can I deserialize this document to a C# class? All samples I have seen assume some structure in nested documents.

The following class gives an error:

public class Report
{
    [BsonId]
    public ObjectId _id { get; set; }

    public string name { get; set; }

    public BsonDocument sub { get; set; }
}

Type 'MongoDB.Bson.BsonString' with data contract name '...' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.

EDIT

What I'm trying to do might be complete non-sense. Is it a better idea to just use one BsonDocument and handle everything manually without a structured class?

4
  • try using a dynamic keyword.This way your unstructured data will be mapped to a complementing c# object at run time.msdn.microsoft.com/en-us/library/dd264736.aspx Commented Oct 9, 2012 at 11:18
  • I don't think dynamic is properly supported by the official driver yet. I've battled this problem myself too, but I came to the conclusion that my documents weren't as ad-hoc as I thought. Can you describe what schema variances you have? Or is it simply that your documents are uneven. The difference being that schema variances would mean a totally different structure (different types) whereas uneven might just mean there are sparse documents. Is it possible for instance to describe the uber-document that is a superset of all variants? I also use Dictionary<,> a lot for completely variable objs Commented Oct 9, 2012 at 12:38
  • The problem is I can only define the first level structure, but none of the nested objects. Reason is that each user can define their own document structure and will have a collection of several "document-templates". So strongly typed objects are definitely out. I'm new to C#, but it seems to me Dictionary is also out because it's still structured. Commented Oct 10, 2012 at 3:35
  • Anyway, I feel I'm asking the wrong question. Maybe I should not even try to map anything, but just use a BsonDocument or JsonSerializer. What I have to do with the Json structure is 1. consolidate/split json objects from different mongodb collections 2. CRUD via http-api 3. Different scheduled processes that run queries on the mongodb Commented Oct 10, 2012 at 3:37

1 Answer 1

3

I don't think the error message you are getting is from the C# driver. Can you please provide a stack trace?

I've tried to reproduce your issue but it works fine with my test program.

The document inserted by the above test program looks like this:

> db.test.find()
{ "_id" : ObjectId("5075fc6ee447ad1354c1f018"), "name" : "John Doe", "sub" : { "x" : 1, "y" : 2 } }
>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Robert! You are right, the problem was not the de-serialization to the class, which works fine, but probably the content negotiation of WebAPI. If I change my controller to return a string instead of the class, it works. I suppose the api cannot handle content negotiation with the type BsonDocument. My new problem is that without content negotiation I get a "double-processed" json string which looks like this: "{ \"_id\" : ObjectId(\"50768abb1934070e98030647\"), \"
Will start a new question, I think you answered it so far

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.