10

I've got a model that looks like:

public class Record
{
    public Record()
    {
        Created = DateTime.Now;
    }

    public string Id { get; set; }

    public string ApplicationId { get; set; }

    public Dictionary<string, dynamic> Values { get; set; }

    public DateTime Created { get; set; }
}

that's stored in MongoDB using the MongoDB C# Driver. This works GREAT when I do things like:

{
    applicationId: "52f52db8008a280f583c9ff6",
    values: {
     "52f9410a324e68055f39f8c0": 245234
     "52f941096d82a531e700e56b": "sdfasdf"
     "52fa4a96e0b2e0d4e61d0a03": "52fa4a9b91c477e367ab99e6"
    }
}

but when I try to add an array of strings like:

{
    applicationId: "52f52db8008a280f583c9ff6",
    values: {
     "52f9410a324e68055f39f8c0": 245234
     "52f941096d82a531e700e56b": "sdfasdf"
     "52fa4a96e0b2e0d4e61d0a03": "52fa4a9b91c477e367ab99e6"
     "52fa85ea16f04a6151e4ea51": [ "52fa85f2d2ffa4cbdcf538e8", "52fa85f205505f671f3d0d7b"]
    }
}

It gives me the following error when I try to do a GET on the document:

An exception of type 'System.IO.FileFormatException' occurred in MongoDB.Driver.dll but was not handled in user code
Additional information: An error occurred while deserializing the Values property of class API.Models.Record.Record: Cannot create an abstract class.

if I look at the database it saved it but its really funky looking:

enter image description here

anyone had any experience with dynamics and mongo?

5
  • Which UI is that? for mongo... Commented Feb 11, 2014 at 22:46
  • robomongo - robomongo.org Commented Feb 11, 2014 at 22:48
  • Also, you realize serializing is only half the problem, deserializing would be extremely tough if not impossible. Commented Feb 11, 2014 at 22:52
  • @I3arnon - Yes I do, however, the values are serialized 100% fine from JS to C#, its the from Mongo to C# that is causing the problem. Commented Feb 11, 2014 at 22:55
  • See aswer from Rolf stackoverflow.com/questions/10222472/… Commented May 5, 2014 at 12:45

1 Answer 1

7

Update: dynamic is now supported by the v2.0 driver.


You can't use dynamic and MongoDB's C# driver. Here's a the jira ticket about it.

From the description:

We have not yet determined in which version we might add support for C# dynamic types. This ticket is a place where you can comment on the desirability of the feature and/or vote for it.

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

7 Comments

@amcdnl maybe that could help: roysvork.wordpress.com/2013/04/22/…
actually looks like someone fixed it: github.com/mongodb/mongo-csharp-driver/commit/…
@amcdnl That guy is Craig Wilson.. he develops the C# driver. Anyways.. it looks like that feature is done, but the version as a whole isn't yet.
couldn't one just convert a dynamic object to BsonDocument?
I just addressed this by converting the dynamic object to an ExpandoObject, from there you can pass it to the BsonDocument constructor
|

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.