Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.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
Java MongoDB驱动程序:如何更新集合中的所有文档?_Java_Mongodb_Mongo Java Driver - Fatal编程技术网

Java MongoDB驱动程序:如何更新集合中的所有文档?

Java MongoDB驱动程序:如何更新集合中的所有文档?,java,mongodb,mongo-java-driver,Java,Mongodb,Mongo Java Driver,以下代码允许我们更新customerDetail集合中customer\u user\u id为1的所有文档: db.getCollection("customerDetail") .updateMany(Filters.eq("customer_user_id", 1), Updates.combine( Updates.set("birth_yea

以下代码允许我们更新
customerDetail
集合中
customer\u user\u id
1
的所有文档:

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")
                ));
第一个是条件-因为它是空的-相当于
{}
-表示所有文档


第二个是要为所有匹配文档设置的文档

我经常使用的一个选项

mongoCollectionObject
        .updateMany(new Document(), //
                new Document("$set"
                        new Document("birth_year", "birth_year")
                        .append("country", "country")
                ));
第一个是条件-因为它是空的-相当于
{}
-表示所有文档


第二个是要为所有匹配文档设置的文档

为什么不将
new document()
作为第一个参数发送?为什么不将
new document()
作为第一个参数发送?