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.
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.
I tried this code:
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();
But it seems that this approach is based on an outdated MongoDB Java Driver, such as 2.4 and uses deprecated methods.
My question:
How to perform a bulk update of documents in MongoDB 3.2 with MongoDB Java Driver 3.2?
解决方案
Using the example in the manual on the new
{ "_id" : 1, "char" : "Brisbane", "class" : "monk", "lvl" : 4 },
{ "_id" : 2, "char" : "Eldon", "class" : "alchemist", "lvl" : 3 },
{ "_id" : 3, "char" : "Meldane", "class" : "ranger", "lvl" : 3 }
The following
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); }
which prints the output:
{
"acknowledged" : true,
"deletedCount" : 1,
"insertedCount" : 2,
"matchedCount" : 2,
"upsertedCount" : 0,
"insertedIds" : {
"0" : 4,
"1" : 5
},
"upsertedIds" : {
}
}
The equivalent Java 3.2 implementation follows:
MongoCollection collection = db.getCollection("characters");
List> writes = new ArrayList>();
writes.add(
new InsertOneModel(
new Document("_id", 4)
.append("char", "Dithras")
.append("class", "barbarian")
.append("lvl", 3)
)
);
writes.add(
new InsertOneModel(
new Document("_id", 5)
.append("char", "Taeln")
.append("class", "fighter")
.append("lvl", 4)
)
);
writes.add(
new UpdateOneModel(
new Document("char", "Eldon"), // filter
new Document("$set", new Document("status", "Critical Injury")) // update
)
);
writes.add(new DeleteOneModel(new Document("char", "Brisbane")));
writes.add(
new ReplaceOneModel(
new Document("char", "Meldane"),
new Document("char", "Tanys")
.append("class", "oracle")
.append("lvl", 4)
)
);
BulkWriteResult bulkWriteResult = collection.bulkWrite(writes);
For your question use the
MongoCollection collection = db.getCollection("collection");
List> writes = Arrays.>asList(
new ReplaceOneModel(
new Document("_id", 1001), // filter
new Document("author", "newName"), // update
new UpdateOptions().upsert(true) // options
)
);
BulkWriteResult bulkWriteResult = collection.bulkWrite(writes);