简体   繁体   English

使用Yaml和MongoDB的问题

[英]Problems using yaml and mongodb

I have a yaml file (third party file that I dont have control over). 我有一个yaml文件(我无法控制的第三方文件)。 I need to get this into my mongodb. 我需要把它放入我的mongodb中。 But when I try inserting it into my collection, I get the below exception 但是当我尝试将其插入我的收藏夹时,出现以下异常

java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.String

Am using snakeyaml to convert my yaml into a Map. 我正在使用snakeyaml将我的Yaml转换为Map。 One of my nested structure in yaml has keys as double values, and this is what I think is causing the confusion in mongo db. 我在yaml中的嵌套结构之一将键作为双精度值,这就是我认为引起mongo db混乱的原因。

Am using mongo-java-driver-2.11.3 to interface with mongo 我正在使用mongo-java-driver-2.11.3与mongo交互

Here is a sample of my yaml file 这是我的yaml文件的示例

persons:
  - 1st relative:
      name: Alice
      sons:
        - 0:
            name: Alice First son
        - 0.2:
            name: Alice Second son
        - 0.3:
            name: Alice Third son
        - 0.4:
            name: Alice Fourth son
        - 0.5:
           name: Alice Fifth son

And here is my simple code to insert into mongo 这是我插入mongo的简单代码

 Yaml yaml = new Yaml();
        Map<String, Object> object =  (Map<String, Object>) yaml.load(new FileInputStream\\test.yaml")));
        coll.insert(new BasicDBObject(object));

Any ideas on how to get around this would be much appreciated 任何关于如何解决这个问题的想法将不胜感激

Thanks K 谢谢K

Apparently, this problem seems to have gone away when I used mongo-java-driver-2.10.0. 显然,当我使用mongo-java-driver-2.10.0时,此问题似乎已消失。 I had to go to that version because of my play version which is 2.10.0. 因为我的播放版本是2.10.0,所以我不得不去那个版本。

Even though am puzzled as to why it has gone away, am happy it is no more an issue. 即使对为什么它消失了感到困惑,也很高兴这不再是一个问题。

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

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