I'm getting the following error when retrieving a document from MongoDB:
"Unable to determine actual type of object to deserialize. NominalType is System.Object and BsonType is Array."
The object that I'm serializing has a Dictionary<string, object>
property. I can put a simple string in the Dictionary and pull it out without getting an error, but if there is a List<string>
then I get the deserialization error.
I'm using the official c# driver (v 1.1). I can query the document just fine using Mongo shell, so I'm pretty it's an issue with MongoDB.Bson.
Any suggestions/workarounds?
Code sample as requested:
Example object being saved to MongoDB:
public class WebUser
{
public int _id;
private DateTime startTime;
private DateTime stopTime;
private string browser;
private string sessionID;
private string ip;
public List<PageView> PageViews;
public Dictionary<string, Object> Session;
public Save(){/*Data access code here*/}
public static Single(int id){/*Data access code here*/}
}
Data Access Code:
public T Single<T>(int id)开发者_JS百科 where T : class, new()
{
var server = MongoServer.Create(ConnectionString);
var db = server.GetDatabase(DBName);
var collection = db.GetCollection<T>(typeof(T).Name);
var value = collection.FindOneById(id);
server.Disconnect();
return value;
}
This works just fine:
var wu = WebUser.single(1);
wu.Session.Add("string key", "value");
wu.Session.Add("int key", 1);
wu.Save();
wu = WebUser.single(1);
This is where I get an error:
var wu = WebUser.single(1);
wu.Session.Add("list of values", new List<string>() { "yada", "yada 2", "yada 3" });
wu.Save();
//deserialize error on the retrieve below
wu = WebUser.single(1);
I think the following simple example reproduces the issue. Use class:
public class C {
public ObjectId Id;
public object Obj;
}
and the following test code:
collection.RemoveAll();
var c = new C { Obj = new int[] { 1, 2, 3 } };
collection.Insert(c);
var r = collection.FindOneAs<C>(); // fails
The problem is that the document was serialized as:
> db.test.find()
{ "_id" : ObjectId("4e15b931e447ad6a54eb0114"), "Obj" : [ 1, 2, 3 ] }
>
and the value for "Obj" has no type information, so the deserializer doesn't know what class to instantiate for "Obj".
I've create a JIRA ticket for this:
https://jira.mongodb.org/browse/CSHARP-263
精彩评论