9

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?

i3arnon
  • 113,022
  • 33
  • 324
  • 344
amcdnl
  • 8,470
  • 12
  • 63
  • 99

1 Answers1

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.

i3arnon
  • 113,022
  • 33
  • 324
  • 344
  • @amcdnl maybe that could help: http://roysvork.wordpress.com/2013/04/22/using-linq-to-query-loosely-typed-data-in-mongodb/ – i3arnon Feb 11 '14 at 23:58
  • actually looks like someone fixed it: https://github.com/mongodb/mongo-csharp-driver/commit/a9b7afb7aa25d4e7e0bc29b4e7fc28d9688af9f0 – amcdnl Feb 13 '14 at 16:30
  • 1
    @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. – i3arnon Feb 13 '14 at 23:23
  • couldn't one just convert a dynamic object to BsonDocument? – BlackTigerX Feb 14 '14 at 19:57
  • One probably could, if the architecture fits you. But I haven't had much practice with it. – i3arnon Feb 14 '14 at 22:26
  • @BlackTigerX - problem is I'm using MongoRepository too which handles a lot of the 'nitty gritty' like that – amcdnl Feb 19 '14 at 14:32
  • 1
    I just addressed this by converting the dynamic object to an ExpandoObject, from there you can pass it to the BsonDocument constructor – BlackTigerX Feb 19 '14 at 15:42