概述
MongoDB支持執行批量更新和插入操作,允許在一次操作中插入或檢索多個文檔。通過使用Batch接口,減少客戶端和數據庫之間的調用次數,可以顯著提高數據庫訪問性能。
本文將演示如何使用MongoDB Shell和JAVA代碼兩種方式實現文檔的批量更新。
數據庫初始化
首先,我們需要連接到mongo shell:
mongo --host localhost --port 27017
建立一個數據庫testDb和一個populations集合:
use testDb;
db.createCollection(populations);
使用insertMany方法將一些樣本數據添加到集合中:
db.populations.insertMany([
{
"cityId":1124,
"cityName":"New York",
"countryName":"United States",
"continentName":"North America",
"population":22
},
{
"cityId":1125,
"cityName":"Mexico City",
"countryName":"Mexico",
"continentName":"North America",
"population":25
},
{
"cityId":1126,
"cityName":"New Delhi",
"countryName":"India",
"continentName":"Asia",
"population":45
},
{
"cityId":1134,
"cityName":"London",
"countryName":"England",
"continentName":"Europe",
"population":32
}]);
執行上述insertMany查詢將返回以下文檔:
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("623575049d55d4e137e477f6"),
ObjectId("623575049d55d4e137e477f7"),
ObjectId("623575049d55d4e137e477f8"),
ObjectId("623575049d55d4e137e477f9")
]
}
使用MongoDB Shell查詢
MongoDB的批量操作生成器用于為單個集合批量構建寫操作列表,可以使用insert、update、replace和remove等方法執行不同類型的操作:
db.populations.bulkWrite([
{
insertOne :
{
"document" :
{
"cityId":1128,
"cityName":"Kathmandu",
"countryName":"Nepal",
"continentName":"Asia",
"population":12
}
}
},
{
insertOne :
{
"document" :
{
"cityId":1130,
"cityName":"Mumbai",
"countryName":"India",
"continentName":"Asia",
"population":55
}
}
},
{
updateOne :
{
"filter" :
{
"cityName": "New Delhi"
},
"update" :
{
$set :
{
"status" : "High Population"
}
}
}
},
{
updateMany :
{
"filter" :
{
"cityName": "London"
},
"update" :
{
$set :
{
"status" : "Low Population"
}
}
}
},
{
deleteOne :
{
"filter" :
{
"cityName":"Mexico City"
}
}
},
{
replaceOne :
{
"filter" :
{
"cityName":"New York"
},
"replacement" :
{
"cityId":1124,
"cityName":"New York",
"countryName":"United States",
"continentName":"North America",
"population":28
}
}
}
]);
上述bulkWrite查詢將返回以下文檔:
{
"acknowledged" : true,
"deletedCount" : 1,
"insertedCount" : 2,
"matchedCount" : 3,
"upsertedCount" : 0,
"insertedIds" :
{
"0" : ObjectId("623575f89d55d4e137e477f9"),
"1" : ObjectId("623575f89d55d4e137e477fa")
},
"upsertedIds" : {}
}
使用Java進行批量操作
首先創建一個MongoClient連接:
MongoClient mongoClient = new MongoClient("localhost", 27017);
MongoDatabase database = mongoClient.getDatabase("testDb");
MongoCollection<Document> collection = database.getCollection("populations");
使用Java代碼實現相同的批量操作:
List<WriteModel<Document>> writeOperations = new ArrayList<WriteModel<Document>>();
writeOperations.add(new InsertOneModel<Document>(new Document("cityId", 1128)
.Append("cityName", "Kathmandu")
.append("countryName", "Nepal")
.append("continentName", "Asia")
.append("population", 12)));
writeOperations.add(new InsertOneModel<Document>(new Document("cityId", 1130)
.append("cityName", "Mumbai")
.append("countryName", "India")
.append("continentName", "Asia")
.append("population", 55)));
writeOperations.add(new UpdateOneModel<Document>(new Document("cityName", "New Delhi"),
new Document("$set", new Document("status", "High Population"))
));
writeOperations.add(new UpdateManyModel<Document>(new Document("cityName", "London"),
new Document("$set", new Document("status", "Low Population"))
));
writeOperations.add(new DeleteOneModel<Document>(new Document("cityName", "Mexico City")));
writeOperations.add(new ReplaceOneModel<Document>(new Document("cityId", 1124),
new Document("cityName", "New York").append("cityName", "United States")
.append("continentName", "North America")
.append("population", 28)));
BulkWriteResult bulkWriteResult = collection.bulkWrite(writeOperations);
System.out.println("bulkWriteResult:- " + bulkWriteResult);
首先創建了一個writeModel列表,將所有不同類型的寫操作添加到一個更新列表中。此外,我們在查詢中使用了InsertOneModel、UpdateOneModel、UpdateManyModel、DeleteOneModel和ReplaceOneModel。最后,bulkWrite方法一次執行了所有的操作。
結論
MongoDB的Bulk批量操作詳細資料可以參考:
https://www.mongodb.com/docs/manual/core/bulk-write-operations/