簡體   English   中英

Java MongoDB 驅動程序:如何更新集合中的所有文檔?

[英]Java MongoDB driver: How to update all Documents in Collection?

以下代碼允許我們更新customerDetail集合中customer_user_id1的所有文檔:

db.getCollection("customerDetail")
        .updateMany(Filters.eq("customer_user_id", 1),
                Updates.combine(
                        Updates.set("birth_year", "birth_year"),
                        Updates.set("country", "country")
                ));

但我需要更新集合中的所有文檔,所以我需要找到一種方法來詢問 Java 驅動程序不應用任何過濾器來更新查詢,但正如我所看到的updateMany方法Filter是一個強制屬性,我不能只需通過null

那么如何更新所有文檔呢?

我經常使用的一個選項

mongoCollectionObject
        .updateMany(new Document(), //
                new Document("$set"
                        new Document("birth_year", "birth_year")
                        .append("country", "country")
                ));

第一個是條件 - 因為它是空的 - 相當於{} - 表示所有文檔

第二個是要為所有匹配文檔設置的文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM