Search code examples
node.jsarraysmongodbchangestream

Get delta of nested array element in a document using change stream (Node.js)


I have a document that has an array field called telephone which can have multiple extension array objects that can have multiple objects in it. So its an array inside an array. I am listening to the db using changeStream. If I change telephone[0].extension[0].valueBoolean = true where telephone[0].extension[0].url == "https//google.com", I get the whole telephone array back in change.updateDescription.updatedFields NOT just telephone[0].extension[0]

updatedFields

{
    "telephone": [{
            "use": "offline",
            "extension": [
            {
                "url": "https//gmail.com",
                "valueDateTime": "2021-01-12T06:31:48.000Z"
            }, {
                "url": "https//yahoo.com",
                "valueDateTime": "1700-01-01T00:00:00.000Z"
            }, {
                "url": "https//google.com",
                "TimeLastModified": "2021-02-23T11:06:06.000Z",
                "valueBoolean": false
            }],
            "value": "+123456789",
            "system": "phone"
        }, {
            "use": "internet",
            "extension": [
            {
                "url": "https//gmail.com",
                "valueDateTime": "2021-01-12T06:31:48.000Z"
            }, {
                "url": "https//yahoo.com",
                "valueDateTime": "1700-01-01T00:00:00.000Z"
            }, {
                "url": "https//google.com",
                "TimeLastModified": "2021-02-23T11:06:06.000Z",
                "valueBoolean": false
            }],
            "value": "+123456799",
            "system": "phone"
        }]
    }

Here's what i have so far


MongoClient.connect(CONNECTION_STRING, {
  useUnifiedTopology: true,
})
  .then((client) => {
    console.log("Connected successfully to server");
      dbConnected = true;
    }
    // specify db and collections
    const db = client.db(DB_NAME);
    const myCollection = db.collection(COLLECTION_NAME);

    const options = { fullDocument: "updateLookup" };

    const changeStream = myCollection.watch(options);
    // start listening to changes
    changeStream.on("change", async (change) => {
      // console.log("CHANGE!");
      // console.log(JSON.stringify(change));
      // check operationType
      try {
        if (
          change.operationType == "insert" ||
          change.operationType == "update" ||
          change.operationType == "replace"
        ) {
          const updatedFields = change.updateDescription.updatedFields
          console.log("updatedFields", JSON.stringify(change.updateDescription.updatedFields));
        }
      } catch (e) {
        console.log(e);
      }
    });
  })
  .catch((e) => {
    console.log(`Error: ${e}`);
  });

How do I see what exact element in a nested array changed with changeStream ?


Solution

  • Unfortunately it seems that this is currently not supported - there's an open Jira-ticket that is related to your problem, see https://jira.mongodb.org/browse/SERVER-41559 for further details.