I am using MongoDB version 6.0.
I have a MongoDB document structured as follows:
{
"_id": "123456",
"info": {
"area_name": "foo",
"type_name": "bar",
"year": 2024
// other fields
},
"state": 1
// other fields
}
The info
field is a nested document, and I’ve omitted several fields in the example structure above.
I want to use change streams to monitor updates to all fields within the info
nested document, while ignoring changes to other fields such as state
. How can I achieve this?
I am developing with Java and Spring Data MongoDB.
Here are my attempts so far:
final var options = ChangeStreamOptions.builder()
.fullDocumentBeforeChangeLookup(FullDocumentBeforeChange.REQUIRED)
.filter(Aggregation.newAggregation(
Aggregation.match(
where("updateDescription.updatedFields.info").exists(true)
)
))
.build();
reactiveMongoTemplate.changeStream("collection_name", options, Foo.class)
.subscribe(this::doSomething);
For example, I modified the info.year
field, but it didn’t seem to work.
The reason appears to be that the value of the updatedFields
field in ChangeStreamEvent
is info.year
instead of info
, so the filter condition I added in ChangeStreamOptions
didn’t work.