使用更改流获取文档中嵌套数组元素的增量 (Node.js)
Get delta of nested array element in a document using change stream (Node.js)
我有一个文档,它有一个名为 telephone 的数组字段,它可以有多个 extension 数组对象,其中可以有多个对象.所以它是一个数组中的数组。我正在使用 changeStream 收听数据库。如果我更改 telephone[0].extension[0].valueBoolean = true where telephone[0].extension[0].url == "https//google.com",
我在 change.updateDescription.updatedFields NOT 中得到了整个 telephone 数组 只是 telephone[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 发生了变化?
不幸的是,这似乎目前不受支持 - 有一个与您的问题相关的开放式 Jira-ticket,请参阅 https://jira.mongodb.org/browse/SERVER-41559 了解更多详细信息。
我有一个文档,它有一个名为 telephone 的数组字段,它可以有多个 extension 数组对象,其中可以有多个对象.所以它是一个数组中的数组。我正在使用 changeStream 收听数据库。如果我更改 telephone[0].extension[0].valueBoolean = true where telephone[0].extension[0].url == "https//google.com", 我在 change.updateDescription.updatedFields NOT 中得到了整个 telephone 数组 只是 telephone[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 发生了变化?
不幸的是,这似乎目前不受支持 - 有一个与您的问题相关的开放式 Jira-ticket,请参阅 https://jira.mongodb.org/browse/SERVER-41559 了解更多详细信息。