简体   繁体   English

Mongo DB - 列表问题<string>嵌套在字典中<string, object> - c# 驱动程序</string,></string>

[英]Mongo DB - Issue with a List<string> Nested in a Dictionary<string, object>- c# Driver

I'm getting the following error when retrieving a document from MongoDB:从 MongoDB 检索文档时出现以下错误:

"Unable to determine actual type of object to deserialize. NominalType is System.Object and BsonType is Array." “无法确定要反序列化的 object 的实际类型。NominalType 是 System.Object,BsonType 是 Array。”

The object that I'm serializing has a Dictionary<string, object> property.我正在序列化的 object 具有Dictionary<string, object>属性。 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.我可以在 Dictionary 中放入一个简单的字符串并将其拉出而不会出现错误,但如果存在List<string>则会出现反序列化错误。

I'm using the official c# driver (v 1.1).我正在使用官方的 c# 驱动程序(v 1.1)。 I can query the document just fine using Mongo shell, so I'm pretty it's an issue with MongoDB.Bson.我可以使用 Mongo shell 很好地查询文档,所以我很高兴这是 MongoDB.Bson 的问题。

Any suggestions/workarounds?有什么建议/解决方法吗?

Code sample as requested:根据要求的代码示例:

Example object being saved to MongoDB:示例 object 被保存到 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) 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:使用 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".并且“Obj”的值没有类型信息,因此反序列化器不知道要为“Obj”实例化什么 class。

I've create a JIRA ticket for this:我为此创建了一张 JIRA 票证:

https://jira.mongodb.org/browse/CSHARP-263 https://jira.mongodb.org/browse/CSHARP-263

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM