title: MongoDB, Mongoose Change Stream update JS/TS Variable
body: <p>I am trying to use MongoDB's change stream to update my frontend state. The problem I am having is reliably converting the data from MongoDB to a valid JS syntax.</p>
  <p>I can make it work in specific cases, but not a broad number of fields. Here is an example:</p>
  <pre><code>if (browser) {
      const socket = io('ws://localhost:3001');
      socket.on('login', async (data: any) =&gt; {
          console.log(data);
          const updated = data.updateDescription.updatedFields;
          Object.keys(updated).forEach(key =&gt; {
              const a = key.split('.');
                  if (currentValue &amp;&amp; currentValue[a[0]]) {
                      console.log(a[0]);
                      console.log(a[1]);
                      currentValue[a[0]][a[1]] = updated[key];
                  }
          });
          user_store.set(currentValue);
  
      });
  }
  </code></pre>
  <p>This is all well and good. However, it only works for array items on my currentValue. Here is an example of the &quot;data&quot; variable that I am getting:</p>
  <pre><code>{_id: {…}, operationType: 'update', clusterTime: {…}, ns: {…}, documentKey: {…}, …}
  clusterTime:
  $timestamp: &quot;7130732853481963526&quot;
  [[Prototype]]: Object
  documentKey:
  _id: &quot;62c41920958f50a17846ba0f&quot;
  [[Prototype]]: Object
  ns:
  coll: &quot;logins&quot;
  db: &quot;motionwatch&quot;
  [[Prototype]]: Object
  operationType: &quot;update&quot;
  updateDescription:
  removedFields: []
  truncatedArrays: []
  updatedFields:
  services.1: &quot;door&quot;
  [[Prototype]]: Object
  [[Prototype]]: Object
  _id:
  _data: &quot;8262F573C0000000062B022C0100296E5A1004B1C1EDF1B2344FE79D0C6DBADEF808CB46645F6964006462C41920958F50A17846BA0F0004&quot;
  [[Prototype]]: Object
  [[Prototype]]: Object
  </code></pre>
  <p>in this case, <code>services.1: &quot;door&quot;</code> this is an array item, services, being the array name, and 1 being the index (0 based).
  I am doing the split to get these, but if I was to change an item on the root currentValue object, this would not work, and I don't want to do one offs for everything.</p>
  <p>How can I make this better?</p>