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

我正在使用MongoDB 3.2和MongoDB

Java驱动程序3.2。我有一个包含数百个更新文档的数组,这些文档现在应该保存/存储在MongoDB中。为了做到这一点,我遍历数组,并为该数组中的每个文档调用updateOne()方法。

现在,我想通过批量更新重新实现此逻辑。我尝试在使用MongoDB Java驱动程序3.2的MongoDB 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),并使用了不赞成使用的方法。

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

回答:

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

{ "_id" : 1, "char" : "Brisbane", "class" : "monk", "lvl" : 4 },

{ "_id" : 2, "char" : "Eldon", "class" : "alchemist", "lvl" : 3 },

{ "_id" : 3, "char" : "Meldane", "class" : "ranger", "lvl" : 3 }

以下 对characters集合执行多项操作:


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实现如下:

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);


对于您的问题,请使用

方法,并将其实现为

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中执行文档的批量更新 的全部内容, 来源链接: utcz.com/qa/425405.html

回到顶部