private WriteConcernResult translateBulkWriteResult(final BulkWriteResult bulkWriteResult) { return WriteConcernResult.acknowledged(getCount(bulkWriteResult), getUpdatedExisting(bulkWriteResult), bulkWriteResult.getUpserts().isEmpty() ? null : bulkWriteResult.getUpserts().get(0).getId()); }
private UpdateResult toUpdateResult(final com.mongodb.bulk.BulkWriteResult result) { if (result.wasAcknowledged()) { Long modifiedCount = result.isModifiedCountAvailable() ? (long) result.getModifiedCount() : null; BsonValue upsertedId = result.getUpserts().isEmpty() ? null : result.getUpserts().get(0).getId(); return UpdateResult.acknowledged(result.getMatchedCount(), modifiedCount, upsertedId); } else { return UpdateResult.unacknowledged(); } }
private WriteConcernResult translateBulkWriteResult(final WriteRequest.Type type, final BulkWriteResult writeResult) { switch (type) { case INSERT: return WriteConcernResult.acknowledged(writeResult.getInsertedCount(), false, null); case DELETE: return WriteConcernResult.acknowledged(writeResult.getDeletedCount(), false, null); case UPDATE: case REPLACE: return WriteConcernResult.acknowledged(writeResult.getMatchedCount() + writeResult.getUpserts().size(), writeResult.getMatchedCount() > 0, writeResult.getUpserts().isEmpty() ? null : writeResult.getUpserts().get(0).getId()); default: throw new MongoInternalException("Unhandled write request type: " + type); } }
private int getCount(final BulkWriteResult bulkWriteResult) { int count = 0; if (getType() == UPDATE || getType() == REPLACE) { count = bulkWriteResult.getMatchedCount() + bulkWriteResult.getUpserts().size(); } else if (getType() == DELETE) { count = bulkWriteResult.getDeletedCount(); } return count; }
return false; if (!upserts.equals(that.getUpserts())) { return false;
private void addBulkWriteResultToResponse(final BulkWriteResult bulkWriteResult, final BsonDocument response) { response.put("ok", new BsonInt32(1)); if (getType() == INSERT) { response.put("n", new BsonInt32(0)); } else if (getType() == DELETE) { response.put("n", new BsonInt32(bulkWriteResult.getDeletedCount())); } else if (getType() == UPDATE || getType() == REPLACE) { response.put("n", new BsonInt32(bulkWriteResult.getMatchedCount() + bulkWriteResult.getUpserts().size())); if (bulkWriteResult.getUpserts().isEmpty()) { response.put("updatedExisting", BsonBoolean.TRUE); } else { response.put("updatedExisting", BsonBoolean.FALSE); response.put("upserted", bulkWriteResult.getUpserts().get(0).getId()); } } }
/** * Add a result * * @param result the result * @param indexMap the index map */ public void addResult(final BulkWriteResult result, final IndexMap indexMap) { insertedCount += result.getInsertedCount(); matchedCount += result.getMatchedCount(); deletedCount += result.getDeletedCount(); if (result.isModifiedCountAvailable() && modifiedCount != null) { modifiedCount += result.getModifiedCount(); } else { modifiedCount = null; } mergeUpserts(result.getUpserts(), indexMap); }
static BulkWriteResult translateBulkWriteResult(final com.mongodb.bulk.BulkWriteResult bulkWriteResult, final Decoder<DBObject> decoder) { if (bulkWriteResult.wasAcknowledged()) { Integer modifiedCount = (bulkWriteResult.isModifiedCountAvailable()) ? bulkWriteResult.getModifiedCount() : null; return new AcknowledgedBulkWriteResult(bulkWriteResult.getInsertedCount(), bulkWriteResult.getMatchedCount(), bulkWriteResult.getDeletedCount(), modifiedCount, translateBulkWriteUpserts(bulkWriteResult.getUpserts(), decoder)); } else { return new UnacknowledgedBulkWriteResult(); } }
private WriteConcernResult translateBulkWriteResult(final BulkWriteResult bulkWriteResult) { return WriteConcernResult.acknowledged(getCount(bulkWriteResult), getUpdatedExisting(bulkWriteResult), bulkWriteResult.getUpserts().isEmpty() ? null : bulkWriteResult.getUpserts().get(0).getId()); }
private UpdateResult toUpdateResult(final com.mongodb.bulk.BulkWriteResult result) { if (result.wasAcknowledged()) { Long modifiedCount = result.isModifiedCountAvailable() ? (long) result.getModifiedCount() : null; BsonValue upsertedId = result.getUpserts().isEmpty() ? null : result.getUpserts().get(0).getId(); return UpdateResult.acknowledged(result.getMatchedCount(), modifiedCount, upsertedId); } else { return UpdateResult.unacknowledged(); } }
private UpdateResult toUpdateResult(final com.mongodb.bulk.BulkWriteResult result) { if (result.wasAcknowledged()) { Long modifiedCount = result.isModifiedCountAvailable() ? (long) result.getModifiedCount() : null; BsonValue upsertedId = result.getUpserts().isEmpty() ? null : result.getUpserts().get(0).getId(); return UpdateResult.acknowledged(result.getMatchedCount(), modifiedCount, upsertedId); } else { return UpdateResult.unacknowledged(); } }
private WriteConcernResult translateBulkWriteResult(final WriteRequest.Type type, final BulkWriteResult writeResult) { switch (type) { case INSERT: return WriteConcernResult.acknowledged(writeResult.getInsertedCount(), false, null); case DELETE: return WriteConcernResult.acknowledged(writeResult.getDeletedCount(), false, null); case UPDATE: case REPLACE: return WriteConcernResult.acknowledged(writeResult.getMatchedCount() + writeResult.getUpserts().size(), writeResult.getMatchedCount() > 0, writeResult.getUpserts().isEmpty() ? null : writeResult.getUpserts().get(0).getId()); default: throw new MongoInternalException("Unhandled write request type: " + type); } }
private WriteConcernResult translateBulkWriteResult(final WriteRequest.Type type, final BulkWriteResult writeResult) { switch (type) { case INSERT: return WriteConcernResult.acknowledged(writeResult.getInsertedCount(), false, null); case DELETE: return WriteConcernResult.acknowledged(writeResult.getDeletedCount(), false, null); case UPDATE: case REPLACE: return WriteConcernResult.acknowledged(writeResult.getMatchedCount() + writeResult.getUpserts().size(), writeResult.getMatchedCount() > 0, writeResult.getUpserts().isEmpty() ? null : writeResult.getUpserts().get(0).getId()); default: throw new MongoInternalException("Unhandled write request type: " + type); } }
public <T extends Document> void bulkUpsertSessions(Collection<SessionEvent> events) { final BulkWriteResult res = sessions().bulkWrite(events.stream().map(event -> { if (event instanceof DeleteSessionEvent) { return new DeleteOneModel<T>(new Document("_id", event.getSessionId())); } else if (event instanceof UpdateSessionEvent) { return new UpdateOneModel<T>( new Document("_id", event.getSessionId()).append("object.sessionId", event.getSessionId()), new Document("$set", new Document("object.$.timestamp", event.getTimestamp())) ); } else { return new UpdateOneModel<T>( new Document("_id", event.getSessionId()), new Document("$set", new Document("object", serializer.toDBObject(event).get(OBJECT_FIELD))), new UpdateOptions().upsert(true) ); } }).collect(toList())); LOGGER.info("Sessions update in bulk results: {} created, {} updated, {} deleted, {} upserted", res.getInsertedCount(), res.getModifiedCount(), res.getDeletedCount(), res.getUpserts().size()); }
private int getCount(final BulkWriteResult bulkWriteResult) { int count = 0; if (getType() == UPDATE || getType() == REPLACE) { count = bulkWriteResult.getMatchedCount() + bulkWriteResult.getUpserts().size(); } else if (getType() == DELETE) { count = bulkWriteResult.getDeletedCount(); } return count; }
private void addBulkWriteResultToResponse(final BulkWriteResult bulkWriteResult, final BsonDocument response) { response.put("ok", new BsonInt32(1)); if (getType() == INSERT) { response.put("n", new BsonInt32(0)); } else if (getType() == DELETE) { response.put("n", new BsonInt32(bulkWriteResult.getDeletedCount())); } else if (getType() == UPDATE || getType() == REPLACE) { response.put("n", new BsonInt32(bulkWriteResult.getMatchedCount() + bulkWriteResult.getUpserts().size())); if (bulkWriteResult.getUpserts().isEmpty()) { response.put("updatedExisting", BsonBoolean.TRUE); } else { response.put("updatedExisting", BsonBoolean.FALSE); response.put("upserted", bulkWriteResult.getUpserts().get(0).getId()); } } }
/** * Add a result * * @param result the result * @param indexMap the index map */ public void addResult(final BulkWriteResult result, final IndexMap indexMap) { insertedCount += result.getInsertedCount(); matchedCount += result.getMatchedCount(); deletedCount += result.getDeletedCount(); if (result.isModifiedCountAvailable() && modifiedCount != null) { modifiedCount += result.getModifiedCount(); } else { modifiedCount = null; } mergeUpserts(result.getUpserts(), indexMap); }
if (wr.getUpserts() != null) { nrep.addProperty("inserted", new BsonInt32(wr.getUpserts().size())); wr.getUpserts().stream(). forEach(update -> { nrep.addLink(
static BulkWriteResult translateBulkWriteResult(final com.mongodb.bulk.BulkWriteResult bulkWriteResult, final Decoder<DBObject> decoder) { if (bulkWriteResult.wasAcknowledged()) { Integer modifiedCount = (bulkWriteResult.isModifiedCountAvailable()) ? bulkWriteResult.getModifiedCount() : null; return new AcknowledgedBulkWriteResult(bulkWriteResult.getInsertedCount(), bulkWriteResult.getMatchedCount(), bulkWriteResult.getDeletedCount(), modifiedCount, translateBulkWriteUpserts(bulkWriteResult.getUpserts(), decoder)); } else { return new UnacknowledgedBulkWriteResult(); } }
@Test public void testBulkUpsert() throws Exception { List<ReplaceOneModel<Document>> models = Arrays.asList( new ReplaceOneModel<>(Filters.eq("_id", 1), json("_id: 1, a: 1"), new ReplaceOptions().upsert(true)), new ReplaceOneModel<>(Filters.eq("_id", 2), json("_id: 2, a: 1"), new ReplaceOptions().upsert(true)) ); BulkWriteResult result = collection.bulkWrite(models, new BulkWriteOptions().ordered(false)); assertThat(result.getUpserts()) .extracting(BulkWriteUpsert::getId) .containsExactly(new BsonInt32(1), new BsonInt32(2)); assertThat(toArray(collection.find())) .containsExactlyInAnyOrder(json("_id: 1, a: 1"), json("_id: 2, a: 1")); models = Arrays.asList( new ReplaceOneModel<>(Filters.eq("_id", 1), json("_id: 1, a: 2"), new ReplaceOptions().upsert(true)), new ReplaceOneModel<>(Filters.eq("_id", 3), json("_id: 3, a: 2"), new ReplaceOptions().upsert(true)), new ReplaceOneModel<>(Filters.eq("_id", 2), json("_id: 2, a: 2"), new ReplaceOptions().upsert(true)) ); result = collection.bulkWrite(models, new BulkWriteOptions().ordered(false)); assertThat(result.getUpserts()) .extracting(BulkWriteUpsert::getId) .containsExactly(new BsonInt32(3)); assertThat(toArray(collection.find())) .containsExactlyInAnyOrder( json("_id: 1, a: 2"), json("_id: 2, a: 2"), json("_id: 3, a: 2") ); }