我有MongoDB文件,如:
{_id:1001, "title":"abc", "author":"xyz"}
为100万个此类文档更新作者的最佳方法是什么?我开始了解MongoDB中的无序批量更新。如何使用Mongo的Java驱动程序实现它。
答案 0 :(得分:1)
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"));
//append all other documents
builder.execute();