Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用mongodb Java驱动程序更新旧文档?_Java - Fatal编程技术网

如何使用mongodb Java驱动程序更新旧文档?

如何使用mongodb Java驱动程序更新旧文档?,java,Java,我试图在mongoDB中实现对现有文档的检查,然后需要使用Java驱动程序3.4对其进行更新(不是重复,只是旧文档) 我的代码片段是: // search feed Document found = database.getCollection("rss_feed").find(new Document("title", title) .append("url", url) .append("img", img)

我试图在mongoDB中实现对现有文档的检查,然后需要使用Java驱动程序3.4对其进行更新(不是重复,只是旧文档)

我的代码片段是:

 // search feed
        Document found = database.getCollection("rss_feed").find(new Document("title", title)
                .append("url", url)
                .append("img", img)
                .append("price", price)).first();
        if (found == null) {
            collection.insertOne(new Document("title", title)
                    .append("url", url)
                    .append("img", img)
                    .append("price", price));
                     mongoClient.close();
        System.out.println("Feed not exists in database. Written.");
        } else {
            System.out.println("Feed exists in database.");
        mongoClient.close();
        }
但通过这种方式,我只添加了新文档,而不进行更新。 (据我所知,当集群在溢出“文档大小”期间自动检查并删除较旧的文档时,这样做很好。)

当我试图通过添加签入其他条件更新旧文档时:

  // search feed
        Document found = database.getCollection("rss_feed").find(new Document("title", title)
                .append("url", url)
                .append("img", img)
                .append("price", price)).first();
        if (found == null) {
            collection.insertOne(new Document("title", title)
                    .append("url", url)
                    .append("img", img)
                    .append("price", price));
                     mongoClient.close();
        System.out.println("Feed not exists in database. Written.");
        } else {
            collection.updateOne(eq(found), new Document("title", title)
                    .append("url", url)
                    .append("img", img)
                    .append("price", price));
                    mongoClient.close();
            System.out.println("Feed exists in database. Updated");
        mongoClient.close();
        }
我得到:

WARNING: Unsupported option 'retrywrites' in the connection string


更新:

如果我使用
count
方法写入
else If
条件以检查文档数:

else if(collection.count() > 36) {
            collection.updateOne(found, new Document("updated title", title)
                    .append("updated url", url)
                    .append("updated img", img)
                    .append("updated price", price));
            System.out.println("Feed completely updated in database.");
然后将跳过
else if

Feed already exists in database.
Feed already exists in database.
Feed already exists in database.
Feed already exists in database.
Feed already exists in database.

是否有人知道如何编写检查文档来更新这些文档(如果存在)?

MongoDb 3.6中根据添加了
retryWrites
连接参数。正如您自己所说,您正在使用3.4版,因此出现了警告


在代码的某个地方,您可能有一个类似于以下内容的连接字符串:
mongodb://db1.example.net:27017?retryWrites=true
。您需要升级到>=3.6版本,或者从连接字符串中删除
retryWrites
参数,以消除警告。

有几种方法可以实现它:

  • Capped collections自动删除文档中最旧的文档 不需要脚本或显式删除操作的集合

  • 使用方法
  • 要替换文档,请将新文档传递给replaceOne方法

  • 使用/方法。 在我的例子中,我只需要更新几个字段,所以我这样写:

    updateOne(eq(“\u id”,user\u id),新文档(“$set”,新文档(“查看链接”,“打开”))

  • 在某些情况下,您可能需要更新文档中的多个字段,替换文档可能更有效


    谢谢你对我的问题感兴趣,扬尼克。你说得对,我需要避免
    retryWrites
    。但这有点奇怪,因为正如前面提到的mongodb公司,我需要在两个变量中使用
    retryWrites
    参数。
    3.4
    -
    mongodb://admin:@cluster0-shard-00-00-ox90k.mongodb.net:27017,cluster0-shard-00-01-ox90k.mongodb.net:27017,cluster0-shard-00-02-ox90k.mongodb.net:27017/test?ssl=true&replicset=cluster0-shard-0&authSource=admin&retryWrites=true
    3.6
    -
    mongodb+srv://admin:@cluster0-ox90k.mongodb.net/test?retryWrites=true
    但在这两种变体中,即使避免了错误,我也无法正确更新抱歉,我认为问题在于警告。你说“无法正确更新”是什么意思?你有实际的错误吗?或者更新只是没有反映在数据库中?没有错,这个答案也很有帮助。:)此错误实际上是由连接参数引起的。不要担心retryWrites警告。可以忽略它。最好从连接字符串中删除。更新部分呢?我不确定你到底在更新什么。它看起来与插入/查找相同。你有更多的密码吗?@Veeram,谢谢你在这里检查我的答案。我正试图找到一种方法来更新/删除我的文档,如果它们已经存在并且需要更新的话。例如,我有36个包含信息的文档,所以我的解析器检查它并尝试在新文档中更新它们。欢迎您。还不清楚。您是否尝试更新到不同的值?现在它不会更新任何内容,因为上一个文档没有任何更改。此外,您不能将
    found
    直接传递到
    Filters.eq
    。尝试
    collection.updateOne(找到,新文档(“标题”,“更新标题”)。这应该在db中显示。@Veeram,这个变体对我没有帮助,很遗憾(什么也没发生)。我还更新了我的问题,尝试实现文档更新数量超过固定数量。您是否可以尝试
    collection.replaceOne(找到,新文档(“标题”,“更新标题”)