简体   繁体   中英

System.FormatException while trying to deserialize BsonDocument to a class

I'm completely new to mongoDB and I'm learning the basics. I'm using .NET driver (2.7.2) of mongoDB and Robo 3T as an utility program to manage databases manually. I made a TestDB database where I've created myCollection where I have many test documents like this :

/* 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
}

and so on ... In my console app (in Visual Studio 2017 community) I've created two classes like this :

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; }
}

I would like to get a document from the database which is marked as /* 3 */ in my example data which is more complex. Program where I try to get the data in main method is like this :

    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();
    }

When I start the app I get this error : System.FormatException:"An error occurred while deserializing the info property of class TEST_MongoDB_consoleApp.MyClass: Cannot deserialize a 'List<Info>' from BsonType 'Document'."

What am I doing wrong ?

I've also created this class (for less complex documents in myCollection ) :

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

and I can deserialize it and get it from database without any problems when doing this the same way with MyClass which is like this :

    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();
    }

In this case program runs without any exeption and I get correct ObjectID value from a document.

Again - what am I doing wrong in case of MyClass ?

This will help. In Mongodb, for every entry in a collection, one needs to have a objectid followed by fields.

Need to change your info class like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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