如何使用Java在MongoDB中执行批量更新文档

编程入门 行业动态 更新时间:2024-10-22 23:06:31
本文介绍了如何使用Java在MongoDB中执行批量更新文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用 MongoDB 3.2和 MongoDB Java驱动程序 3.2。我有一个数百个更新文档的数组,现在应保存/存储在 MongoDB 中。为了做到这一点,我迭代数组并在此数组中调用 updateOne()方法中的每个文档。

I'm using MongoDB 3.2 and MongoDB Java Driver 3.2. I have an array of a couple of hundreds of updated documents which should be now saved/stored in MongoDB. In order to do that, I iterate over the array and call for each document in this array the updateOne() method.

现在,我想通过批量更新重新实现此逻辑。我尝试使用 MongoDB Java驱动程序 3.2找到 MongoDB 3.2中批量更新的示例。

Now, I want to re-implement this logic with a bulk update. I tried to find an example of bulk update in MongoDB 3.2 with MongoDB Java Driver 3.2.

我试过这段代码:

MongoClient mongo = new MongoClient("localhost", 27017); DB db = (DB) mongo.getDB("test1"); DBCollection collection = db.getCollection("collection"); BulkWriteOperation builder = collection.initializeUnorderedBulkOperation(); builder.find(new BasicDBObject("_id", 1001)).upsert() .replaceOne(new BasicDBObject("_id", 1001).append("author", "newName")); builder.execute();

但似乎这种方法基于过时的 MongoDB Java驱动程序,例如2.4并使用弃用的方法。

But it seems that this approach is based on an outdated MongoDB Java Driver, such as 2.4 and uses deprecated methods.

我的问题: 如何执行使用 MongoDB Java驱动程序批量更新MongoDB 3.2中的文档 3.2?

推荐答案

使用新手册中的示例 bulkWrite() API,请考虑以下包含以下文档的测试集合:

Using the example in the manual on the new bulkWrite() API, consider the following test collection which contains the following documents:

{ "_id" : 1, "char" : "Brisbane", "class" : "monk", "lvl" : 4 }, { "_id" : 2, "char" : "Eldon", "class" : "alchemist", "lvl" : 3 }, { "_id" : 3, "char" : "Meldane", "class" : "ranger", "lvl" : 3 }

以下 bulkWrite() 对字符执行多项操作集合:

Mongo shell:

try { db.characters.bulkWrite([ { insertOne:{ "document":{ "_id" : 4, "char" : "Dithras", "class" : "barbarian", "lvl" : 4 } } }, { insertOne:{ "document": { "_id" : 5, "char" : "Taeln", "class" : "fighter", "lvl" : 3 } } }, { updateOne: { "filter" : { "char" : "Eldon" }, "update" : { $set : { "status" : "Critical Injury" } } } }, { deleteOne: { "filter" : { "char" : "Brisbane"} } }, { replaceOne: { "filter" : { "char" : "Meldane" }, "replacement" : { "char" : "Tanys", "class" : "oracle", "lvl" : 4 } } } ]); } catch (e) { print(e); }

打印输出:

{ "acknowledged" : true, "deletedCount" : 1, "insertedCount" : 2, "matchedCount" : 2, "upsertedCount" : 0, "insertedIds" : { "0" : 4, "1" : 5 }, "upsertedIds" : { } }

等效的Java 3.2实现如下:

The equivalent Java 3.2 implementation follows:

MongoCollection<Document> collection = db.getCollection("characters"); List<WriteModel<Document>> writes = new ArrayList<WriteModel<Document>>(); writes.add( new InsertOneModel<Document>( new Document("_id", 4) .append("char", "Dithras") .append("class", "barbarian") .append("lvl", 3) ) ); writes.add( new InsertOneModel<Document>( new Document("_id", 5) .append("char", "Taeln") .append("class", "fighter") .append("lvl", 4) ) ); writes.add( new UpdateOneModel<Document>( new Document("char", "Eldon"), // filter new Document("$set", new Document("status", "Critical Injury")) // update ) ); writes.add(new DeleteOneModel<Document>(new Document("char", "Brisbane"))); writes.add( new ReplaceOneModel<Document>( new Document("char", "Meldane"), new Document("char", "Tanys") .append("class", "oracle") .append("lvl", 4) ) ); BulkWriteResult bulkWriteResult = collection.bulkWrite(writes);

对于您的问题,请使用 replaceOne() 方法,这将实现为

MongoCollection<Document> collection = db.getCollection("collection"); List<WriteModel<Document>> writes = Arrays.<WriteModel<Document>>asList( new ReplaceOneModel<Document>( new Document("_id", 1001), // filter new Document("author", "newName"), // update new UpdateOptions().upsert(true) // options ) ); BulkWriteResult bulkWriteResult = collection.bulkWrite(writes);

更多推荐

如何使用Java在MongoDB中执行批量更新文档

本文发布于:2023-11-28 10:11:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641981.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   批量   文档   MongoDB   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!