更新特定文档时如何从 mongoDb 获取完整文档?
how to get fullDocument from mongoDb when partlicular document is updated?
实际上,我正在 Node.js (Express.js) 中处理变更流。
我陷入了以下情况。
这里我使用 mongoose
与 mongoDB 建立了数据库连接
async function main() {
const uri = process.env.DATABASE;
const client = mongoose.connect(uri, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
reconnectInterval: 500, // Reconnect every 500m
})
// Connect to the MongoDB cluster
await client
.then(() => console.log('DB Connected'))
.catch(er => console.log(er));
const pipeline = [
{
'$match': {
$or: [{ operationType: 'insert' }, { operationType: 'update' }]
}
}
];
await monitorListingsUsingEventEmitter(client, 30000,pipeline);
}
main().catch(console.error);
这是改变流的函数
async function monitorListingsUsingEventEmitter(client, timeInMs = 60000, pipeline = []) {
const changeStream = await members.watch(pipeline);
changeStream.on('change', async (next) => {
switch (next.operationType) {
case 'insert':
console.log('an insert happened...', "uni_ID: ", next.fullDocument);
break;
case 'update':
console.log('an update happened...', next.fullDocument);
//
//getting undefined in above line because there is no property name 'fullDocument' in object next
//
break;
case 'delete':
console.log('a delete happened...');
break;
default:
break;
}
});
总的来说,我的问题是:
I need a full document object that is getting updated without any query
我得到了答案。
我刚刚检查了 https://www.mongodb.com/docs/manual/reference/method/db.collection.watch/.
其实我用过
const changeStream = await members.watch(pipeline);
上面的语句应该是这样的。
它现在工作
const changeStream = await members.watch(pipeline, { fullDocument: "updateLookup" }
//this actual answer
感谢大家不回覆:)
我终于做到了
实际上,我正在 Node.js (Express.js) 中处理变更流。 我陷入了以下情况。
这里我使用 mongoose
与 mongoDB 建立了数据库连接async function main() {
const uri = process.env.DATABASE;
const client = mongoose.connect(uri, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
reconnectInterval: 500, // Reconnect every 500m
})
// Connect to the MongoDB cluster
await client
.then(() => console.log('DB Connected'))
.catch(er => console.log(er));
const pipeline = [
{
'$match': {
$or: [{ operationType: 'insert' }, { operationType: 'update' }]
}
}
];
await monitorListingsUsingEventEmitter(client, 30000,pipeline);
}
main().catch(console.error);
这是改变流的函数
async function monitorListingsUsingEventEmitter(client, timeInMs = 60000, pipeline = []) {
const changeStream = await members.watch(pipeline);
changeStream.on('change', async (next) => {
switch (next.operationType) {
case 'insert':
console.log('an insert happened...', "uni_ID: ", next.fullDocument);
break;
case 'update':
console.log('an update happened...', next.fullDocument);
//
//getting undefined in above line because there is no property name 'fullDocument' in object next
//
break;
case 'delete':
console.log('a delete happened...');
break;
default:
break;
}
});
总的来说,我的问题是:
I need a full document object that is getting updated without any query
我得到了答案。 我刚刚检查了 https://www.mongodb.com/docs/manual/reference/method/db.collection.watch/.
其实我用过
const changeStream = await members.watch(pipeline);
上面的语句应该是这样的。 它现在工作
const changeStream = await members.watch(pipeline, { fullDocument: "updateLookup" }
//this actual answer
感谢大家不回覆:) 我终于做到了