MixedBulkWriteOperation updateOne(final Bson filter, final Bson update, final UpdateOptions updateOptions) { return bulkWrite(singletonList(new UpdateOneModel<TDocument>(filter, update, updateOptions)), new BulkWriteOptions().bypassDocumentValidation(updateOptions.getBypassDocumentValidation())); }
private WriteModel<Document> mapWriteModel(WriteModel<Document> writeModel) { if (writeModel instanceof UpdateOneModel) { UpdateOneModel<Document> model = (UpdateOneModel<Document>) writeModel; return new UpdateOneModel<>(getMappedQuery(model.getFilter()), getMappedUpdate(model.getUpdate()), model.getOptions()); } if (writeModel instanceof UpdateManyModel) { UpdateManyModel<Document> model = (UpdateManyModel<Document>) writeModel; return new UpdateManyModel<>(getMappedQuery(model.getFilter()), getMappedUpdate(model.getUpdate()), model.getOptions()); } if (writeModel instanceof DeleteOneModel) { DeleteOneModel<Document> model = (DeleteOneModel<Document>) writeModel; return new DeleteOneModel<>(getMappedQuery(model.getFilter()), model.getOptions()); } if (writeModel instanceof DeleteManyModel) { DeleteManyModel<Document> model = (DeleteManyModel<Document>) writeModel; return new DeleteManyModel<>(getMappedQuery(model.getFilter()), model.getOptions()); } return writeModel; }
} else if (writeModel instanceof UpdateOneModel) { UpdateOneModel<TDocument> updateOneModel = (UpdateOneModel<TDocument>) writeModel; writeRequest = new UpdateRequest(toBsonDocument(updateOneModel.getFilter()), toBsonDocument(updateOneModel.getUpdate()), WriteRequest.Type.UPDATE) .multi(false) .upsert(updateOneModel.getOptions().isUpsert()) .collation(updateOneModel.getOptions().getCollation()) .arrayFilters(toBsonDocumentList(updateOneModel.getOptions().getArrayFilters())); } else if (writeModel instanceof UpdateManyModel) { UpdateManyModel<TDocument> updateManyModel = (UpdateManyModel<TDocument>) writeModel;
@Test @DisplayName("when valid doc change cdc event then correct UpdateOneModel") public void testValidSinkDocumentForUpdate() { BsonDocument keyDoc = new BsonDocument("id",new BsonString("1004")); BsonDocument valueDoc = new BsonDocument("op",new BsonString("u")) .append("patch",new BsonString(UPDATE_DOC.toJson())); WriteModel<BsonDocument> result = MONGODB_UPDATE.perform(new SinkDocument(keyDoc,valueDoc)); assertTrue(result instanceof UpdateOneModel, () -> "result expected to be of type UpdateOneModel"); UpdateOneModel<BsonDocument> writeModel = (UpdateOneModel<BsonDocument>) result; assertEquals(UPDATE_DOC,writeModel.getUpdate(), ()-> "update doc not matching what is expected"); assertTrue(writeModel.getFilter() instanceof BsonDocument, () -> "filter expected to be of type BsonDocument"); assertEquals(FILTER_DOC,writeModel.getFilter()); }
} else if (writeModel instanceof UpdateOneModel) { UpdateOneModel<TDocument> updateOneModel = (UpdateOneModel<TDocument>) writeModel; writeRequest = new UpdateRequest(toBsonDocument(updateOneModel.getFilter()), toBsonDocument(updateOneModel.getUpdate()), WriteRequest.Type.UPDATE) .multi(false) .upsert(updateOneModel.getOptions().isUpsert()) .collation(updateOneModel.getOptions().getCollation()) .arrayFilters(toBsonDocumentList(updateOneModel.getOptions().getArrayFilters())); } else if (writeModel instanceof UpdateManyModel) { UpdateManyModel<TDocument> updateManyModel = (UpdateManyModel<TDocument>) writeModel;
new ArrayList<UpdateOneModel<Document>>(bulkInserts.size()); for (Document doc : bulkInserts) { updates.add(new UpdateOneModel<Document>( new Document("_id", doc.get("_id")), doc, UPDATE_WITH_UPSERT));
private WriteModel<Document> mapWriteModel(WriteModel<Document> writeModel) { if (writeModel instanceof UpdateOneModel) { UpdateOneModel<Document> model = (UpdateOneModel<Document>) writeModel; return new UpdateOneModel<>(getMappedQuery(model.getFilter()), getMappedUpdate(model.getUpdate()), model.getOptions()); } if (writeModel instanceof UpdateManyModel) { UpdateManyModel<Document> model = (UpdateManyModel<Document>) writeModel; return new UpdateManyModel<>(getMappedQuery(model.getFilter()), getMappedUpdate(model.getUpdate()), model.getOptions()); } if (writeModel instanceof DeleteOneModel) { DeleteOneModel<Document> model = (DeleteOneModel<Document>) writeModel; return new DeleteOneModel<>(getMappedQuery(model.getFilter()), model.getOptions()); } if (writeModel instanceof DeleteManyModel) { DeleteManyModel<Document> model = (DeleteManyModel<Document>) writeModel; return new DeleteManyModel<>(getMappedQuery(model.getFilter()), model.getOptions()); } return writeModel; }
BsonDocument updateDoc = (BsonDocument)writeModel.getUpdate(); () -> "modified and inserted timestamps must initially be equal"); assertTrue(writeModel.getFilter() instanceof BsonDocument, () -> "filter expected to be of type BsonDocument"); assertEquals(FILTER_DOC_UPDATE_TIMESTAMPS,writeModel.getFilter()); assertTrue(writeModel.getOptions().isUpsert(), () -> "update expected to be done in upsert mode");
/** * Performs update and upsert bulk operations. * * @param query the {@link Query} to determine documents to update. * @param update the {@link Update} to perform, must not be {@literal null}. * @param upsert whether to upsert. * @param multi whether to issue a multi-update. * @return the {@link BulkOperations} with the update registered. */ private BulkOperations update(Query query, Update update, boolean upsert, boolean multi) { Assert.notNull(query, "Query must not be null!"); Assert.notNull(update, "Update must not be null!"); UpdateOptions options = new UpdateOptions(); options.upsert(upsert); query.getCollation().map(Collation::toMongoCollation).ifPresent(options::collation); if (multi) { models.add(new UpdateManyModel<>(query.getQueryObject(), update.getUpdateObject(), options)); } else { models.add(new UpdateOneModel<>(query.getQueryObject(), update.getUpdateObject(), options)); } return this; }
/** * Performs update and upsert bulk operations. * * @param query the {@link Query} to determine documents to update. * @param update the {@link Update} to perform, must not be {@literal null}. * @param upsert whether to upsert. * @param multi whether to issue a multi-update. * @return the {@link BulkOperations} with the update registered. */ private BulkOperations update(Query query, Update update, boolean upsert, boolean multi) { Assert.notNull(query, "Query must not be null!"); Assert.notNull(update, "Update must not be null!"); UpdateOptions options = new UpdateOptions(); options.upsert(upsert); query.getCollation().map(Collation::toMongoCollation).ifPresent(options::collation); if (multi) { models.add(new UpdateManyModel<>(query.getQueryObject(), update.getUpdateObject(), options)); } else { models.add(new UpdateOneModel<>(query.getQueryObject(), update.getUpdateObject(), options)); } return this; }
private UpdateOneModel<Document> createRevisionUpdate(final Bson thingFilter, final long targetRevision) { final Document update = new Document(PersistenceConstants.SET, new Document(PersistenceConstants.FIELD_REVISION, targetRevision)); return new UpdateOneModel<>(thingFilter, update); }
private UpdateOneModel<Document> createRevisionUpdate(final Bson thingFilter, final long targetRevision) { final Document update = new Document(PersistenceConstants.SET, new Document(PersistenceConstants.FIELD_REVISION, targetRevision)); return new UpdateOneModel<>(thingFilter, update); }
private static List<UpdateOneModel<Document>> createThingIndexModels(final Bson filter, final PolicyUpdate update) { final List<UpdateOneModel<Document>> updates = new ArrayList<>(3); final Bson pullGlobalReads = update.getPullGlobalReads(); if (pullGlobalReads != null) { updates.add(new UpdateOneModel<>(filter, pullGlobalReads)); } final Bson pushGlobalReads = update.getPushGlobalReads(); if (pushGlobalReads != null) { updates.add(new UpdateOneModel<>(filter, pushGlobalReads)); } final Bson pullAclEntries = update.getPullAclEntries(); if (pullAclEntries != null) { updates.add(new UpdateOneModel<>(filter, pullAclEntries)); } return updates; }
private static List<UpdateOneModel<Document>> createThingIndexModels(final Bson filter, final PolicyUpdate update) { final List<UpdateOneModel<Document>> updates = new ArrayList<>(3); final Bson pullGlobalReads = update.getPullGlobalReads(); if (pullGlobalReads != null) { updates.add(new UpdateOneModel<>(filter, pullGlobalReads)); } final Bson pushGlobalReads = update.getPushGlobalReads(); if (pushGlobalReads != null) { updates.add(new UpdateOneModel<>(filter, pushGlobalReads)); } final Bson pullAclEntries = update.getPullAclEntries(); if (pullAclEntries != null) { updates.add(new UpdateOneModel<>(filter, pullAclEntries)); } return updates; }
@Override public void saveStates(List<State> list) { MongoDbOperations.doDbOperation(() -> { List<WriteModel<Document>> operations = new ArrayList<>(); for (State state : list) { operations.add(new UpdateOneModel<>(new Document("clz", state.getSagaStateId()).append("sid", state.getId()), new Document("$set",new Document("s", state.getState())),new UpdateOptions().upsert(true))); } final BulkWriteResult bulkWriteResult = states.bulkWrite(operations); return bulkWriteResult; }); } }
private void insertUpdateInBulkNoMatch(boolean ordered) { collection.insertOne(json("foo: 'bar'")); List<WriteModel<Document>> ops = new ArrayList<>(); ops.add(new UpdateOneModel<Document>(ne("foo", "bar"), set("field", "y"))); BulkWriteResult result = collection.bulkWrite(ops, new BulkWriteOptions().ordered(ordered)); assertThat(result.getInsertedCount()).isEqualTo(0); assertThat(result.getDeletedCount()).isEqualTo(0); assertThat(result.getModifiedCount()).isEqualTo(0); assertThat(result.getMatchedCount()).isEqualTo(0); }
private <T extends ThingEvent> List<WriteModel<Document>> createThingUpdates(final Bson filter, final T thingEvent) { final List<Bson> updates = persistenceStrategyFactory .getStrategy(thingEvent) .thingUpdates(thingEvent, IndexLengthRestrictionEnforcer.newInstance(log, thingEvent.getThingId())); return updates .stream() .map(update -> new UpdateOneModel<Document>(filter, update, new UpdateOptions().upsert(true))) .collect(Collectors.toList()); }
private <T extends ThingEvent> List<WriteModel<Document>> createThingUpdates(final Bson filter, final T thingEvent) { final List<Bson> updates = persistenceStrategyFactory .getStrategy(thingEvent) .thingUpdates(thingEvent, IndexLengthRestrictionEnforcer.newInstance(log, thingEvent.getThingId())); return updates .stream() .map(update -> new UpdateOneModel<Document>(filter, update, new UpdateOptions().upsert(true))) .collect(Collectors.toList()); }
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()); }
MixedBulkWriteOperation updateOne(final Bson filter, final Bson update, final UpdateOptions updateOptions) { return bulkWrite(singletonList(new UpdateOneModel<TDocument>(filter, update, updateOptions)), new BulkWriteOptions().bypassDocumentValidation(updateOptions.getBypassDocumentValidation())); }