使用MongoDB Java 3.0驱动程序批量Upsert

在早期版本的MongoDB Java驱动程序中,要运行查询并对结果进行无序批量upsert,我们所做的就是:

BulkWriteOperation bulk = dbCollection.initializeUnorderedBulkOperation(); bulk.find(searchQuery).upsert().update(new BasicDBObject("$set", getDbObjectModel())); 

但是在版本3中,随着Bson Document支持和MongoCollection.bulkWrite()方法的引入,如何才能做到这一点?

我试过这个:

 List<WriteModel> documentList = new ArrayList(); collection.bulkWrite(documentList, new BulkWriteOptions().ordered(false)); 

但是,我需要upsertfunction。

谢谢。

您仍然可以使用所有function,只是BulkWrites现在有不同的语法:

  MongoCollection collection = db.getCollection("sample"); List> updates = Arrays.>asList( new UpdateOneModel( new Document(), // find part new Document("$set",1), // update part new UpdateOptions().upsert(true) // options like upsert ) ); BulkWriteResult bulkWriteResult = collection.bulkWrite(updates); 

因此,您可以使用UpdateOneModel (如果需要,可以使用UpdateOneModel )并将UpdateOptions设置为构造函数的第三个参数。

需要一些时间来习惯,但它基本上只是用与其他地方相同的语法构建“列表”。 我猜这是改变的主要原因。

以下是使用最新API的示例..

 for (Long entityId : entityIDs) { //Finder doc Document filterDocument = new Document(); filterDocument.append("_id", entityId); //Update doc Document updateDocument = new Document(); Document setDocument = new Document(); setDocument.append("name", "xyz"); setDocument.append("role", "abc"); updateDocument.append("$set", setDocument); //Update option UpdateOptions updateOptions = new UpdateOptions(); updateOptions.upsert(true); //if true, will create a new doc in case of unmatched find updateOptions.bypassDocumentValidation(true); //set true/false //Prepare list of Updates updateDocuments.add( new UpdateOneModel( filterDocument, updateDocument, updateOptions)); } //Bulk write options BulkWriteOptions bulkWriteOptions = new BulkWriteOptions(); bulkWriteOptions.ordered(false); bulkWriteOptions.bypassDocumentValidation(true); MongoCollection mongoCollection = mongoDB.getCollection("myCollection"); BulkWriteResult bulkWriteResult = null; try { //Perform bulk update bulkWriteResult = mongoCollection.bulkWrite(updateDocuments, bulkWriteOptions); } catch (BulkWriteException e) { //Handle bulkwrite exception List bulkWriteErrors = e.getWriteErrors(); for (BulkWriteError bulkWriteError : bulkWriteErrors) { int failedIndex = bulkWriteError.getIndex(); Long failedEntityId = entityIDs.get(failedIndex); System.out.println("Failed record: " + failedEntityId); //handle rollback } } int rowsUpdated = bulkWriteResult.getModifiedCount(); 

详细信息: http : //ashutosh-srivastav-mongodb.blogspot.in/2017/09/mongodb-bulkwrite-java-api.html

如果你想要一些findAndModifyElseCreate(); 这意味着如果文档存在然后更新它,则创建它并插入数据然后请关注此事。

 BasicDBObject insertInCaseDocumentNotFound = new BasicDBObject(); insertInCaseDocumentNotFound.put("field1", "value1"); insertInCaseDocumentNotFound.put("date", new Date()); MongoCollection table = db.getCollection("collectionName",BasicDBObject.class); BasicDBObject updateObject = new BasicDBObject(); updateObject.append("$setOnInsert", new BasicDBObject()); updateObject.append("$set", new BasicDBObject("date",new Date()); List> updates = Arrays.> asList( new UpdateOneModel(new BasicDBObject("search_name", alert.getString("search_name")), // query for which we need to apply updateObject, // update the document in case it is found new UpdateOptions().upsert(true) // upsert to insert new data in case document is not found )); table.bulkWrite(updates);