Node.js 使用mongoose的管道Mongodb变更流
我正在尝试使用变更流监听我的mongodb集群中的变更;然而,在完成了几篇教程之后,我使用mongoose的最终实现不起作用。如何使用当前的mongoose连接侦听数据库中的更改 猫鼬连接:Node.js 使用mongoose的管道Mongodb变更流,node.js,mongodb,mongoose,changestream,Node.js,Mongodb,Mongoose,Changestream,我正在尝试使用变更流监听我的mongodb集群中的变更;然而,在完成了几篇教程之后,我使用mongoose的最终实现不起作用。如何使用当前的mongoose连接侦听数据库中的更改 猫鼬连接: mongoose .connect(db, { useNewUrlParser: true, useFindAndModify: false, useUnifiedTopology: true // useCreateIndex: true }) .then(() =>
mongoose
.connect(db, {
useNewUrlParser: true,
useFindAndModify: false,
useUnifiedTopology: true
// useCreateIndex: true
})
.then(() => {
console.log("Connected to MongoDB...");
})
.catch(err => {
console.log(err);
});
const pipeline = {
$match: {
$or: [{ operationType: 'insert' },{ operationType: 'update' }],
'fullDocument.institution': uniId
}
};
const changeStream = Post.watch([pipeline], {fullDocument: 'updateLookup'});
changeStream.on("change", next => {
switch(next.operationType) {
case 'insert':
console.log('an insert happened...', "uni_ID: ", next.fullDocument.institution);
let rooms = Object.keys(socket.rooms);
console.log("rooms: ", rooms);
nmsps.emit('insert', {
type: 'insert',
msg: 'New question available',
newPost: next.fullDocument
});
break;
case 'update':
console.log('an update happened...');
nmsps.emit('update', {
type: 'update',
postId: next.documentKey._id,
updateInfo: next.updateDescription.updatedFields,
msg: "Question has been updated."
});
break;
case 'delete':
console.log('a delete happened...');
nmsps.emit('delete', {
type: 'delete',
deletedId: next.documentKey._id,
msg: 'Question has been deleted.'
});
break;
default:
break;
}
})
更改流:
mongoose
.connect(db, {
useNewUrlParser: true,
useFindAndModify: false,
useUnifiedTopology: true
// useCreateIndex: true
})
.then(() => {
console.log("Connected to MongoDB...");
})
.catch(err => {
console.log(err);
});
const pipeline = {
$match: {
$or: [{ operationType: 'insert' },{ operationType: 'update' }],
'fullDocument.institution': uniId
}
};
const changeStream = Post.watch([pipeline], {fullDocument: 'updateLookup'});
changeStream.on("change", next => {
switch(next.operationType) {
case 'insert':
console.log('an insert happened...', "uni_ID: ", next.fullDocument.institution);
let rooms = Object.keys(socket.rooms);
console.log("rooms: ", rooms);
nmsps.emit('insert', {
type: 'insert',
msg: 'New question available',
newPost: next.fullDocument
});
break;
case 'update':
console.log('an update happened...');
nmsps.emit('update', {
type: 'update',
postId: next.documentKey._id,
updateInfo: next.updateDescription.updatedFields,
msg: "Question has been updated."
});
break;
case 'delete':
console.log('a delete happened...');
nmsps.emit('delete', {
type: 'delete',
deletedId: next.documentKey._id,
msg: 'Question has been deleted.'
});
break;
default:
break;
}
})
因为mongoose是作为核心模块使用的,所以您可以使用mongodb客户端来查看更改流
连接后:
const client=mongoose.connection.client;
const db=client.db('dbName');
const collection=db.collection('collectionName');
const changeStream=collection.watch();
changeStream.on('change',next=>{
});代码>因为mongoose用作核心模块,所以您可以使用mongodb客户端来查看更改流
连接后:
const client=mongoose.connection.client;
const db=client.db('dbName');
const collection=db.collection('collectionName');
const changeStream=collection.watch();
changeStream.on('change',next=>{
});代码>他是changeStream的工作解决方案代码
const mongoose = require('mongoose')
const { connection } = require('../boot/mongo')
const Schema = mongoose.Schema
const status = new Schema({
_id: {
type: mongoose.Schema.Types.Number,
required: true
},
receiveTs: {
type: mongoose.Schema.Types.Date,
required: true
}
})
const OnlineStatusSchema = connection.model('Status', Status, 'status')
const pipeline = [
{
$match: {
$or: [{ operationType: 'insert' }, { operationType: 'update' }]
}
},
{ $project: { 'fullDocument._id': 1, 'fullDocument.receiveTs': 1 } }
]
const changeStream = OnlineStatusSchema.watch(pipeline)
changeStream.on('change', async (change) => {
// get meters reading log for respective platfrom and date
try {
console.log(change)
} catch (error) {
throw error
}
})
module.exports = OnlineStatusSchema
他是changeStream的工作解决方案代码
const mongoose = require('mongoose')
const { connection } = require('../boot/mongo')
const Schema = mongoose.Schema
const status = new Schema({
_id: {
type: mongoose.Schema.Types.Number,
required: true
},
receiveTs: {
type: mongoose.Schema.Types.Date,
required: true
}
})
const OnlineStatusSchema = connection.model('Status', Status, 'status')
const pipeline = [
{
$match: {
$or: [{ operationType: 'insert' }, { operationType: 'update' }]
}
},
{ $project: { 'fullDocument._id': 1, 'fullDocument.receiveTs': 1 } }
]
const changeStream = OnlineStatusSchema.watch(pipeline)
changeStream.on('change', async (change) => {
// get meters reading log for respective platfrom and date
try {
console.log(change)
} catch (error) {
throw error
}
})
module.exports = OnlineStatusSchema
关闭变更流部分在哪里?请添加它关闭更改流部分在哪里?请加上