繁体   English   中英

尝试将BsonDocument反序列化为类时发生System.FormatException

[英]System.FormatException while trying to deserialize BsonDocument to a class

我对mongoDB完全陌生,并且正在学习基础知识。 我使用mongoDB的.NET驱动程序(2.7.2)和Robo 3T作为实用程序来手动管理数据库。 我创建了一个TestDB数据库,在其中创建了myCollection ,其中有许多这样的测试文档:

/* 1 */
{
    "_id" : ObjectId("5c124ee01c2477487574b212"),
    "x" : 1.0
}

/* 2 */
{
    "_id" : ObjectId("5c17d26369babb0c04610a33"),
    "count" : 1
}

/* 3 */
{
    "_id" : ObjectId("5c17d40f6002a11c44bc2c42"),
    "name" : "MongoDB",
    "type" : "Database",
    "count" : 1,
    "info" : {
        "x" : 203,
        "y" : 102
    }
}

/* 4 */
{
    "_id" : ObjectId("5c17dead9e83de138c71278a"),
    "counter" : 0
}

/* 5 */
{
    "_id" : ObjectId("5c17dead9e83de138c71278b"),
    "counter" : 1
}

/* 6 */
{
    "_id" : ObjectId("5c17dead9e83de138c71278c"),
    "counter" : 2
}

/* 7 */
{
    "_id" : ObjectId("5c17dead9e83de138c71278d"),
    "counter" : 3
}

/* 8 */
{
    "_id" : ObjectId("5c17dead9e83de138c71278e"),
    "counter" : 4
}

/* 9 */
{
    "_id" : ObjectId("5c17dead9e83de138c71278f"),
    "counter" : 5
}

/* 10 */
{
    "_id" : ObjectId("5c17dead9e83de138c712790"),
    "counter" : 6
}

/* 11 */
{
    "_id" : ObjectId("5c17dead9e83de138c712791"),
    "counter" : 7
}

/* 12 */
{
    "_id" : ObjectId("5c17dead9e83de138c712792"),
    "counter" : 8
}

/* 13 */
{
    "_id" : ObjectId("5c17dead9e83de138c712793"),
    "counter" : 9
}

/* 14 */
{
    "_id" : ObjectId("5c17dead9e83de138c712794"),
    "counter" : 10
}

依此类推...在我的控制台应用程序(在Visual Studio 2017社区中)中,我创建了两个这样的类:

public class MyClass
{
    [BsonId]
    public ObjectId ID { get; set; }
    public string name { get; set; }
    public string type { get; set; }
    public int count { get; set; }
    public List<Info> info { get; set; }
}

public class Info
{
    public int x { get; set; }
    public int y { get; set; }
}

我想从数据库中获取一个文档,在我的示例数据中,该文档被标记为/* 3 */ ,这更为复杂。 我尝试在main方法中获取数据的程序是这样的:

    static void Main(string[] args)
    {
        var client = new MongoClient();
        var database = client.GetDatabase("TestDB");
        var collection2 = database.GetCollection<MyClass>("myCollection");


        var o = collection2.Find(i => i.name == "MongoDB").SingleOrDefault();

        Console.ReadLine();
    }

当我启动应用程序时,出现以下错误: System.FormatException:"An error occurred while deserializing the info property of class TEST_MongoDB_consoleApp.MyClass: Cannot deserialize a 'List<Info>' from BsonType 'Document'."

我究竟做错了什么 ?

我还创建了此类(用于myCollection不太复杂的文档):

public class TestModel
{
    [BsonId]
    public ObjectId IDTestmodel { get; set; }
    public int counter { get; set; }
}

并且可以使用MyClass以相同的方式对它进行反序列化并从数据库中毫无问题地获取它,如下所示:

    static void Main(string[] args)
    {
        var client = new MongoClient();
        var database = client.GetDatabase("Test");
        var collection = database.GetCollection<TestModel>("myCollection");

        var o = collection.Find(i => i.counter == 5).SingleOrDefault(); 

        Console.WriteLine(o.IDTestmodel);

        Console.ReadLine();
    }

在这种情况下,程序运行时没有任何异常,我从文档中获取了正确的ObjectID值。

再说一遍-如果是MyClass我在做什么错?

这会有所帮助。 在Mongodb中,对于集合中的每个条目,都需要有一个objectid,后跟字段。

需要更改您的信息类,例如:

public class Info
{
    [BsonId]
    public ObjectId Id {get; set; }
    public int x { get; set; }
    public int y { get; set; }
}

暂无
暂无

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

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