1

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

Konrad Lötzsch
  • 467
  • 7
  • 15
  • can you show a sample document – pvpkiran Jun 04 '18 at 08:37
  • added a sample document – Konrad Lötzsch Jun 04 '18 at 08:42
  • 1
    This looks correct. Do you have a more complete example of a listing? i.e Watching the change stream then modifying the document but the change does not occur. Are you certain you are actually modifying the document? Note that if you attempt to `$set` to the same value that is already present, then that is not actually a modification. You should basically get a response to confirm `nModified` and then expect the change stream event to fire and the filter condition to catch it. The only concern here is the `class type` on the aggregation. I would advise `Document` here and nothing specific – Neil Lunn Jun 04 '18 at 08:47
  • added a link to my github repo – Konrad Lötzsch Jun 04 '18 at 08:59
  • This looks like a bug. I have raised the bug and put more information here. Have a look. https://jira.spring.io/browse/DATAMONGO-1996 – pvpkiran Jun 04 '18 at 14:27

0 Answers0