Imported from kjuhwa/skills-hub (
skills/backend/mongodb-changestream-field-filter/SKILL.md). Install upstream withnpx skills add kjuhwa/skills-hub --skill mongodb-changestream-field-filter. Copyright stays with the author.
MongoDB Change Stream Field Filter
Shape
Listener receives ChangeStreamDocument. For UPDATE, read updateDescription.updatedFields (a BsonDocument of dottedPath → value). Return a boolean isRelevant before handing off to the expensive processor.
Steps
- Register listener via Spring Data MongoDB (reactive or blocking driver).
- Branch on
OperationType:INSERT/DELETE→ always relevant. UPDATE→updatedFields = event.getUpdateDescription().getUpdatedFields().- Exact check:
updatedFields.containsKey("groupId"). - Nested/wildcard:
updatedFields.keySet().stream().anyMatch(k -> k.startsWith("tags.")). - Combine with OR over the "relevant fields" set; return the boolean.
- Not relevant → log TRACE and skip. Relevant → delegate to processor.
- Persist
resumeTokenso reconnection skips already-seen events.
Counter / Caveats
updatedFieldsis keyed by dotted path at the leaf changed. A whole-subdoc replace shows as"parent", not"parent.child"— handle both.- Full-document replaces (
$seton root) may surface every field as "updated"; treat as relevant. - Never skip DELETEs on field filter — there are no fields to inspect.
- Network hiccup / replica-set election → stream goes silent without exception; pair with
mongodb-changestream-resubscribeknowledge.