Realm Object Can't Observe Changes Made from UpdateOne()

Hi,

Good day,

I have found that updateOne didn’t trigger the listener added for Realm object.

projectRef.current.addListener(() => {
  setProject({
    ...project,
    ...projectRef.current.toJSON(),
  });
});

The codes below were run in the same application after upload is completed.

const result = await dbClient
  .db(appDatabase)
  .collection(attachmentDetails.collection)
  .updateOne(
    { _id: attachmentDetails.id },
    {
      $set: { [attachmentPathInDB]: true },
    }
  );

However, the changes applied by UpdateOne was not observed by the listener.
FYI, I can see that the data in the Atlas has already been changed from the UpdateOne call and Realm Logs did record the transaction as well.

{
  "name": "updateOne",
  "arguments": [
    {
      "database": "test_database",
      "collection": "Projects2",
      "query": {
        "_id": "43d84b2a-9d6b-42d3-9eb6-826362ca20a8"
      },
      "update": {
        "$set": {
          "ProjectContent.FocusAreas2.e8b36173-bfa1-4abe-9ab1-0bd4bae8ee99.imageAttachments.b022279e-d999-4a58-917c-027d61266e60.uploadCompleted": true
        }
      }
    }
  ],
  "service": "mongodb-atlas"
}

However, if I change the field manually through Atlas UI, the change event seems to work and the listener is triggered and subsequently updated the project to the latest state.

Please help to advise.

Thanks,
Vincent

My current workaround:

  1. create another dummy field(lastModified) at the root level of the document
  2. create a separate atlas trigger to detect such changes and update the lastModified field to trigger the change

Suspicion:

  • probably the changes is deep down the document thus not able to emit change event?
1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.