【问题标题】:MongoDB Document Update in Java: coding error or driver bug?Java 中的 MongoDB 文档更新:编码错误或驱动程序错误?
【发布时间】:2016-06-19 17:36:42
【问题描述】:

我正在使用 MongoDB Java Driver v3.2.2,并使用以下指令将新的字符串数组字段添加到现有文档中:

myMongoCollection.updateOne(new Document("id", id), Updates.set("arrayField", Arrays.asList(new Document("strField", strValue))));

它会创建以下异常:

org.bson.codecs.configuration.CodecConfigurationException:找不到 类 java.util.Arrays$ArrayList 的编解码器。

我是在编写代码错误还是与建议的驱动程序错误有关 here (link)

如果它与驱动程序错误有关,任何提示我如何轻松获得已修复的最新尚未发布的版本(3.3)(即如何轻松创建 JAR 文件,知道 GIT对我来说是全新的)?

非常感谢, 汤姆

【问题讨论】:

    标签: java mongodb mongodb-query mongodb-java


    【解决方案1】:

    mongodb-java-driver v3.2.2 中,您可以使用Document 代替Updates.set 构建器。另见Updating Documents

    import static com.mongodb.client.model.Filters.eq;
    
    myMongoCollection.updateOne(eq("id", id), new Document("$set", new Document ("arrayField", Arrays.asList(new Document("strField", strValue)))));
    

    或者,如果修复已进入主分支,您可以从 git 构建 jar:

    $ git clone https://github.com/mongodb/mongo-java-driver.git
    $ cd mongo-java-driver
    $ ./gradlew
    

    生成的 jar 将位于 build/libs 目录中。

    【讨论】:

      猜你喜欢
      • 2015-07-23
      • 1970-01-01
      • 2018-07-10
      • 2019-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多