美文网首页
MongoDB存储String[]抛出异常org.bson.co

MongoDB存储String[]抛出异常org.bson.co

作者: 柠檬信息技术有限公司 | 来源:发表于2018-01-15 09:39 被阅读1693次

在存储String[] 到MongoDB的时候抛出了以下异常,最开始以为是传错了对象,后来才发现,是MongoDB的解码器不支持解析String数组导致的,必须将字符串数组转成List才可以。

org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class [Ljava.lang.String;.
    at org.bson.codecs.configuration.CodecCache.getOrThrow(CodecCache.java:46)
    at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:63)
    at org.bson.codecs.configuration.ChildCodecRegistry.get(ChildCodecRegistry.java:51)
    at org.bson.codecs.DocumentCodec.writeValue(DocumentCodec.java:174)
    at org.bson.codecs.DocumentCodec.writeMap(DocumentCodec.java:189)
    at org.bson.codecs.DocumentCodec.writeValue(DocumentCodec.java:172)
    at org.bson.codecs.DocumentCodec.writeMap(DocumentCodec.java:189)
    at org.bson.codecs.DocumentCodec.writeValue(DocumentCodec.java:172)
    at org.bson.codecs.DocumentCodec.writeMap(DocumentCodec.java:189)
    at org.bson.codecs.DocumentCodec.encode(DocumentCodec.java:131)
    at org.bson.codecs.DocumentCodec.encode(DocumentCodec.java:45)
    at org.bson.BsonDocumentWrapper.getUnwrapped(BsonDocumentWrapper.java:189)
    at org.bson.BsonDocumentWrapper.get(BsonDocumentWrapper.java:129)
    at com.mongodb.AggregateIterableImpl.execute(AggregateIterableImpl.java:118)
    at com.mongodb.AggregateIterableImpl.forEach(AggregateIterableImpl.java:107)
    at undefined.AggregationTest.main(AggregationTest.java:27)

将字符串数组String[]转成List可以使用Arrays.asList()方法(位于java.util包下):

Arrays.asList("hello#gogogo#123".split("#"))

参考:
https://samebug.io/exceptions/2485483/org.bson.codecs.configuration.CodecConfigurationException/cant-find-a-codec-for-class-ljavalangstring?soft=false

相关文章

  • MongoDB存储String[]抛出异常org.bson.co

    在存储String[] 到MongoDB的时候抛出了以下异常,最开始以为是传错了对象,后来才发现,是MongoDB...

  • Use of array of zero length

    String[] strings = new String[0];可以避免一些校验。可以直接遍历而不会抛出异常fo...

  • 异常抛出

    本章主要内容 用try语句捕获异常 常见异常处理 用raise 和assert抛出异常 自定义异常 常用异常名 总...

  • 抛出异常

    在方法执行的时候,遇到不匹配的地方,加入异常抛出机制,可以清晰的知道异常点,并很好的解决。 throw new E...

  • 抛出异常

  • 抛出异常

    MSSQL 在存储过程中进程会处理一些逻辑性的错误,如:将RMB转换为USD时,没有查询到想要的汇率 这个时候最好...

  • 异常抛出

    异常抛出之 Throw 抛出一个已知异常 打印效果: 从打印结果来看,一旦发生异常后面的逻辑就不会执行了。 Cat...

  • oracle捕获并抛出异常

    写一个测试用例,用存储过程捕获异常并抛出异常,定位异常信息。 1.新建一张表 2.新新建一个测试的存储过程 调用此...

  • 数据库抛出异常:Incorrect string value:

    https://blog.csdn.net/xenoverse/article/details/82847729 ...

  • 如何解决emoji表情插入MySQL数据库问题

    问题描述: 当存储含有emoji表情时,会抛出类似如下异常: java.sql.SQLException: Inc...

网友评论

      本文标题:MongoDB存储String[]抛出异常org.bson.co

      本文链接:https://www.haomeiwen.com/subject/yfdroxtx.html