使用更改流Node.js获取文档中嵌套数组元素的增量

问题描述

我有一个文档,它有一个名为 telephone 的数组字段,它可以有多个 extension 数组对象,其中可以有多个对象。所以它是一个数组内的数组。我正在使用 changeStream 收听数据库。如果我更改 phone[0].extension[0].valueBoolean = true where phone[0].extension[0].url == "https//google.com",我在 change.updateDescription.updatedFields NOT获取了整个 电话阵列,只是电话 [0].extension[0]

更新字段

{
    "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","value": "+123456799","system": "phone"
        }]
    }

这是我目前所拥有的


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}`);
  });

如何查看使用 changeStream 更改的嵌套数组中的确切元素?

解决方法

很遗憾,目前似乎不支持此功能 - 有一个与您的问题相关的未结 Jira-ticket,请参阅 https://jira.mongodb.org/browse/SERVER-41559 了解更多详情。