本文将演示如何利用MongoDB Shell和Java代码两种办法实现文档的批量更新。

数据库初始化

首先,我们须要连接到mongo shell:

mongo --host localhost --port 27017

建立一个数据库testDb和一个populations凑集:

phpmongo批量更新MongoDB应用Bulk批量更新操作 CSS

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&gt; 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/