Hi, guys,
After reading this thread I want to share my weird case to see if anyone can help, Im having an issue about monitoring changes in a collection.
I have a nested property that I want to monitor, and I used this approach:
for (String property : Fields.PROPERTIES_TO_MONITOR.keySet()) {
orFilters.add(Document.parse(
"{'updateDescription.updatedFields." + property + "': {$exists: true}}")
.toBsonDocument()
);
}
List<Bson> pipeline = List.of(
Aggregates.match(Filters.and(
Filters.in("operationType", Arrays.asList("update", "replace", "insert"))
Filters.or(new ArrayList<>(orFilters))
))
);
where the keyset returns something like this: “phone_number”, “mobile_phone_number”, “settings.communication.notes”, this works for the first two, but not for any nested property.
so, after reading this thread I opted for the getField approach:
var fieldsToMonitor = Fields.PROPERTIES_TO_MONITOR.keySet();
List<Bson> pipeline = List.of(
Aggregates.match(
Filters.and(
Filters.in("operationType", Arrays.asList("update", "replace", "insert")),
Filters.expr(
new Document("$or", fieldsToMonitor.stream()
.map(field -> new Document("$ne", Arrays.asList(
new Document("$type",
new Document("$getField",
new Document("field", field).append("input", "$updateDescription.updatedFields")
)
),
"missing"
))).toList()
)
)
)
)
);
this works great for “phone_number” or “settings.communication.notes”, the weird part is that is NOT working for both at the same time, if I update “phone_number” I can get the updated_fields, with “phone_number” in it, and when I update “settings.communication.notes” I also get the updated_fields with the notes in it, but if I update the two properties, I just get the “phone_number” (and any other NON nested property).
I hope someone can help me with this.
thanks in advance