简体   繁体   中英

MongoDB Deserialization Error of List c#

I am currently using mongoDB to store player data. I store the player data as a class object. The PlayerInfo class has several variables relating to the player, such as their id, total, gold, ect. It also stores a list of another class(character inventory) that has info about each character the player has.

When I try to connect to the database and retrieve information, I get this error :

Exception :An error occurred while deserializing the myCharsList property of class PlayerInfo:

Here is the code for connecting to the database and retrieving info about the player:

        client = new MongoClient (connectionString);
        server = client.GetServer (); 
        database = server.GetDatabase ("myDB");
        playerCollection = database.GetCollection<PlayerInfo> ("Players");
        try {     
            pInfo = playerCollection.AsQueryable<PlayerInfo>().Where<PlayerInfo>(player => player._id == "101112").SingleOrDefault(); 
            totalGold = pInfo.totalGold;
            myCharsList = pInfo.myCharsList;
            Debug.Log ("GOT INFO FROM DATABASE");
        }   catch (Exception exx) {     
            Debug.Log ("Exception :" + exx.Message);  
            try {   
                pInfo = new PlayerInfo ();  
                pInfo._id = "101112";  
                pInfo.myCharsList = new List<myChar> ();
                playerCollection.Insert (pInfo);   
                Debug.Log ("ADDED NEW PLAYER TO DATABASE"); 
            } catch (Exception exxx) {   
                Debug.Log ("Failed to insert into collection of Database " + database.Name);   
                Debug.Log ("Error :" + exx.Message);   
            } 
        }

Here is the code of the PlayerInfo class that I store directly into mongoDB:

public class PlayerInfo {
    public List<myChar> myCharsList { get; set;}
    public int totalGold  { get; set;}
    public int highScoreGems { get; set;}
    public string _id = "101112";
    public string playerName {get; set;}
    public string email {get; set;}
    public string password {get; set;}
}

The code for class that is stored in the list in the PlayerInfo class is this:

    public class myChar : MonoBehaviour
    {
        public string namee;
        public string tagg ;
        public string rarity;
        public int rarityInt;
        public int level;
        public int exp;
        public int health;
        public int attack;
        public float speed;
}

Everything is being stored (I believe correctly) in the database as seen from this picture of my database: 在此处输入图片说明

Any help would be greatly appreciated! Thanks

We cant see what is in MonoBehaviour, you have 15 fields in the myChar in db. So, I would assume there maybe 6 fields that reside in MonoBehaviour. Basically, I would suggest to closely look if the fields match, by data type and name. If serializer can't figure out where to put the field from mongo document you may get this error.

I hope you have already solved your issue.

Firstly, I would add the [BsonIgnoreExtraElements] to your myChar Class just to move away properties that you are not interested in Deserealizing .

The BsonIgnoreExtraElements : Specifies whether extra elements should be ignored when this class is deserialized.

[BsonIgnoreExtraElements]
public class myChar : MonoBehaviour
    {
        public string namee;
         ...

    }

On the other hand, I would take a closer look at each filed type in that class.

For instance: speed in your Class is defined as float but in the DataBase is stored as Double . So, you could check setting it in your class as Double for example.

Or you could use the BsonRepresentation attribute to represent the value in the BSON document. That's another option that may help but I would go first to check if every single type properly matches.

[BsonRepresentation(BsonType.Double)]
public float speed;

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