mongo-kafka icon indicating copy to clipboard operation
mongo-kafka copied to clipboard

Adding DeleteExactKeyStrategy

Open andrewbelu opened this issue 1 year ago • 0 comments

Allows for the Mongo filter opreation to be set to the exact key coming from the key doc rather than having a superfluous _id key.

This is needed for a few use cases I can imagine, but the one in particular is when the key doc is inserting into a sharded collection and already has an _id field with another field and the document.id.strategy is set to PartialValueStrategy for inserts/updates. When deleting, I would like to simply directly set the key (as the key already has the required fields) instead of generating an additional _id key that the DeleteOneDefaultStrategy seems to add:

if (idStrategy instanceof DefaultIdFieldStrategy) { deleteFilter = idStrategy.generateId(document, null).asDocument(); } else { deleteFilter = new BsonDocument(ID_FIELD, idStrategy.generateId(document, null)); }

Notice if the ID strategy is anything other than the default ID strategy, the DeleteOneDefaultStrategy adds the ID_FIELD key.

With this, I can now simply specify the delete.writemodel.strategy as DeleteExactKeyStrategy and it will properly format the filter expression.

I could not find any way to get this behaviour out of the current write strategies.

andrewbelu avatar Mar 15 '24 04:03 andrewbelu