1

我有一个文档,其中有一个名为电话的数组字段,它可以有多个扩展数组对象,其中可以有多个对象。所以它是一个数组中的一个数组。我正在使用 changeStream 收听数据库。如果我更改电话[0].extension[0].valueBoolean = true where phone[0].extension[0].url == "https//google.com",我会在 change.updateDescription 中获得整个电话阵列.updatedFields不仅仅是电话[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",
            "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"
        }]
    }

这是我到目前为止所拥有的


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 发生了变化?

4

1 回答 1

0

不幸的是,目前似乎不支持这 - 有一个与您的问题相关的开放 Jira-ticket,请参阅https://jira.mongodb.org/browse/SERVER-41559了解更多详细信息。

于 2021-02-27T00:24:06.137 回答