Return 对象集合中的特定数组
Return specific array from object collection
所以我将一些数据放入我的套接字中
客户端中的代码是:
useEffect(() => {
const socket = io("http://localhost:5000/api/socket");
socket.on("newThought", (thought) => {
console.log(thought);
});
}, []);
然后我服务器中的代码是
connection.once("open", () => {
console.log("MongoDB database connected");
console.log("Setting change streams");
const thoughtChangeStream = connection.collection("phonenumbers").watch();
thoughtChangeStream.on("change", (change) => {
io.of("/api/socket").emit("newThought", change);
});
});
当我的“phonenumbers”集合中的某些内容发生变化时,我会进入 return 整个集合。我如何才能只获得从集合中的对象更改的数组?
因此,例如,如果在集合中唯一更改的服务是 ID 为“607deefd13c4ebcbcfa0900a”的服务,那么它应该是唯一的 returned 而不是整个集合对象。
watch
方法的选项(第二个)参数的 fullDocument
参数可用于获取描述 update
操作的文档更改的增量:
const thoughtChangeStream = connection.collection("phonenumbers").watch([], {
fullDocument: 'updateLookup'
});
thoughtChangeStream.on("change", (change) => {
io.of("/api/socket").emit("newThought", change);
});
这将 return 这样的响应文档,其中 updateDescription
包含更新修改的字段:
{
_id: {
_data: '8260931772000000012B022C0100296E5A1004ABFC09CB5798444C8126B1DBABB9859946645F696400646082EA7F05B619F0D586DA440004'
},
operationType: 'update',
clusterTime: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1620252530 },
ns: { db: 'yourDatabase', coll: 'yourCollection' },
documentKey: { _id: 6082ea7f05b619f0d586da44 },
updateDescription: {
updatedFields: { updatedField: 'newValue' },
removedFields: []
}
}
注意:这仅适用于 update
操作,不适用于 replace
、delete
、insert
等
另请参阅:
所以我将一些数据放入我的套接字中 客户端中的代码是:
useEffect(() => {
const socket = io("http://localhost:5000/api/socket");
socket.on("newThought", (thought) => {
console.log(thought);
});
}, []);
然后我服务器中的代码是
connection.once("open", () => {
console.log("MongoDB database connected");
console.log("Setting change streams");
const thoughtChangeStream = connection.collection("phonenumbers").watch();
thoughtChangeStream.on("change", (change) => {
io.of("/api/socket").emit("newThought", change);
});
});
当我的“phonenumbers”集合中的某些内容发生变化时,我会进入 return 整个集合。我如何才能只获得从集合中的对象更改的数组?
watch
方法的选项(第二个)参数的 fullDocument
参数可用于获取描述 update
操作的文档更改的增量:
const thoughtChangeStream = connection.collection("phonenumbers").watch([], {
fullDocument: 'updateLookup'
});
thoughtChangeStream.on("change", (change) => {
io.of("/api/socket").emit("newThought", change);
});
这将 return 这样的响应文档,其中 updateDescription
包含更新修改的字段:
{
_id: {
_data: '8260931772000000012B022C0100296E5A1004ABFC09CB5798444C8126B1DBABB9859946645F696400646082EA7F05B619F0D586DA440004'
},
operationType: 'update',
clusterTime: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1620252530 },
ns: { db: 'yourDatabase', coll: 'yourCollection' },
documentKey: { _id: 6082ea7f05b619f0d586da44 },
updateDescription: {
updatedFields: { updatedField: 'newValue' },
removedFields: []
}
}
注意:这仅适用于 update
操作,不适用于 replace
、delete
、insert
等
另请参阅: