简体   繁体   English

列表c#的MongoDB反序列化错误

[英]MongoDB Deserialization Error of List c#

I am currently using mongoDB to store player data.我目前正在使用 mongoDB 来存储玩家数据。 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. PlayerInfo 类有几个与玩家相关的变量,例如他们的 id、total、gold 等。 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:异常:反序列化 PlayerInfo 类的 myCharsList 属性时发生错误:

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:下面是我直接存入 mongoDB 的 PlayerInfo 类的代码:

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:存储在 PlayerInfo 类的列表中的类的代码是这样的:

    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.我们看不到 MonoBehaviour 中的内容,您在 db 的 myChar 中有 15 个字段。 So, I would assume there maybe 6 fields that reside in MonoBehaviour.因此,我假设 MonoBehaviour 中可能有 6 个字段。 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.如果序列化程序无法确定将 mongo 文档中的字段放在哪里,您可能会收到此错误。

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 .首先,我会将[BsonIgnoreExtraElements]添加到您的myChar类中,以移除您对Deserealizing不感兴趣的属性

The BsonIgnoreExtraElements : Specifies whether extra elements should be ignored when this class is deserialized. BsonIgnoreExtraElements :指定在反序列化此类时是否应忽略额外元素。

[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 .例如:您的 Class 中的speed定义为float但在 DataBase 中存储为Double So, you could check setting it in your class as Double for example.因此,您可以检查在您的班级中将其设置为 Double 。

Or you could use the BsonRepresentation attribute to represent the value in the BSON document.或者您可以使用BsonRepresentation 属性来表示 BSON 文档中的值。 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;

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

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