【问题标题】:CurrentBsonType is String, not when CurrentBsonType is ObjectIdCurrentBsonType 是字符串,而不是当 CurrentBsonType 是 ObjectId
【发布时间】:2012-03-18 07:29:59
【问题描述】:

“反序列化Class1类的Property1属性时出错:ReadString只能在CurrentBsonType为String时调用,当CurrentBsonType为ObjectId时不能调用。”

我将 bsonid 放在类的字符串 guid 上并映射类并对其进行序列化,但是当我尝试将对象从 bsondocument 反序列化回类时,它给了我这个错误。

我应该如何解决这个问题?

public class Class1 
{
      [BsonId]
      public string ClassGuid { get; private set; }
}

然后映射类:

if (!BsonClassMap.IsClassMapRegistered(typeof(WordOntology)))
{
      BsonClassMap.RegisterClassMap<WordOntology>();
}

然后反序列化

MongoCursor<Class1> _returnResults = _collection.FindAs<Class1>(_query);

然后循环:

foreach(BsonDocument _document in _returnResults) 
{
    //exception here
}

【问题讨论】:

    标签: mongodb mongodb-.net-driver


    【解决方案1】:

    您的集合中可能有与您的类定义不匹配的现有文档。您可以使用 mongo shell 查找它们(查找具有您期望字符串的 ObjectId 的文档)。

    如果这不能解释,您能否提供您的类声明和无法正确反序列化的示例文档(使用 mongo shell 显示)?

    【讨论】:

    • 那么它现在对你有用了吗?如果您有任何后续问题,请告诉我。
    猜你喜欢
    • 2015-12-13
    • 2021-03-27
    • 1970-01-01
    • 2020-10-25
    • 2013-09-27
    • 2014-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多