如何在 MongoDB 3 和 Java 中使用多个过滤器执行文档的批量更新
How to perform a bulk update of documents with multiple filters in MongoDB 3 with Java
我想像这样进行批量更新:
List<WriteModel<Document>> writes = new ArrayList<WriteModel<Document>>();
writes.add(
new UpdateOneModel<Document>(
new Document("car", "Ferrari"), // filter
new Document("$set", new Document("color", "Black")) // update
)
);
但我想输入多个过滤器。我以为会是这样的:
List<WriteModel<Document>> writes = new ArrayList<WriteModel<Document>>();
writes.add(
new UpdateOneModel<Document>(
new Document(and(eq("car", "Ferrari"), eq("color", "Red"), // filter
new Document("$set", new Document("color", "Black")) // update
)
);
但是显然不行,到处都找不到。有谁知道是否有办法做到这一点?谢谢:)
使用追加添加其他键:
List<WriteModel<Document>> writes = new ArrayList<WriteModel<Document>>();
writes.add(
new UpdateOneModel<Document>(
new Document("car", "Ferrari").append("color", "Red"), // filter
new Document("$set", new Document("color", "Black")) // update
)
);
我想像这样进行批量更新:
List<WriteModel<Document>> writes = new ArrayList<WriteModel<Document>>();
writes.add(
new UpdateOneModel<Document>(
new Document("car", "Ferrari"), // filter
new Document("$set", new Document("color", "Black")) // update
)
);
但我想输入多个过滤器。我以为会是这样的:
List<WriteModel<Document>> writes = new ArrayList<WriteModel<Document>>();
writes.add(
new UpdateOneModel<Document>(
new Document(and(eq("car", "Ferrari"), eq("color", "Red"), // filter
new Document("$set", new Document("color", "Black")) // update
)
);
但是显然不行,到处都找不到。有谁知道是否有办法做到这一点?谢谢:)
使用追加添加其他键:
List<WriteModel<Document>> writes = new ArrayList<WriteModel<Document>>();
writes.add(
new UpdateOneModel<Document>(
new Document("car", "Ferrari").append("color", "Red"), // filter
new Document("$set", new Document("color", "Black")) // update
)
);