According to the documentation of the mongodb changestream, I can watch for updates on specific fields on the collecttion:
const changeStream = collection.watch(
[{
$match: {
$and: [
{ "updateDescription.updatedFields.a": { $exists: true } },
{ operationType: "update" }
]
}
}],
{
fullDocument: "updateLookup"
}
);
I would like to know, how to achieve the same thing with spring data mongodb reactive.
When I build a change stream with
Flux<ChangeStreamEvent<TestObject>> changeStream =
mongoTemplate.changeStream(
newAggregation(
match(
where("operationType").is("update")
.and("updateDescription").exists(true)
)
),
TestObject.class,
ChangeStreamOptions.empty(),
"testObject"
);
I get all updates. A log of one of these change events looks like:
ChangeStreamEvent {
raw=ChangeStreamDocument{
resumeToken={ "_data" : { "$binary" : "glsU9sYAAAATRmRfaWQAZFsU9sayZFockO0phgBaEATGaP42X4VI+4SrTyscwRtsBA==", "$type" : "00" } },
namespace=test.testObject,
fullDocument=Document{{_id=5b14f6c6b2645a1c90ed2986, a=a, b=b, _class=com.example.demo.TestObject}},
documentKey={ "_id" : { "$oid" : "5b14f6c6b2645a1c90ed2986" } },
clusterTime=null,
operationType=OperationType{value='update'},
updateDescription=UpdateDescription{removedFields=[], updatedFields={ "b" : "b" }}},
targetType=class com.example.demo.TestObject
}
However, when I change the filter to
newAggregation(
match(
where("operationType").is("update")
.and("updateDescription.updatedFields").exists(true)
)
)
or
newAggregation(
match(
where("operationType").is("update")
.and("updateDescription.updatedFields.b").exists(true)
)
)
I get not change events. So here is the question: how can I define the Criteria to match updates on updatedField.b?
A sample Document from the collection is:
{
"_id" : ObjectId("5b14f049b2645a4a24b33df1"),
"a" : "va",
"b" : "vb",
"_class" : "com.example.demo.TestObject"
}
The example with the first "working" filter can be downloaded at: my test github repo