New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using watch
only sends insertion events, not delete or update
#14524
Comments
If you remove the options object in the watch() function, is it working? |
@FaizBShah it works when I have this: const s = Member.watch(); But not with this: const s = Member.watch([
{
$match: {"fullDocument.user_id": user_id},
}
]); Any clue what might be causing it? I'm probably going to just switch to once watch stream anyways to save resources. |
@AlexanderEpolite Im not too familiar with the type of the argument passed in the |
const mongoose = require('mongoose');
const testSchema = new mongoose.Schema({
user_id: mongoose.Schema.Types.ObjectId,
name: String
});
const Test = mongoose.model('Test', testSchema);
async function run() {
await mongoose.connect(process.env.MONGOOSECONNECTIONSTRING);
await mongoose.connection.dropDatabase();
const userId = new mongoose.Types.ObjectId();
const s = Test.watch([
{
$match: {"fullDocument.user_id": userId },
}
]);
s.on('change', (change) => console.log('change', change))
const doc = await Test.create({
user_id: userId,
name: 'Test Testerson'
});
const res = await Test.updateOne({ _id: doc._id }, { name: 'Test' });
console.log('what is res', res);
console.log('done');
}
run(); |
This is expected behavior, you need to set the const s = Member.watch([
{
$match: {"fullDocument.user_id": user_id, "operationType": {$in: ["insert", "update", "delete"]}},
}
], { fullDocument: 'updateLookup' }); |
Prerequisites
Mongoose version
8.3.1
Node.js version
20
MongoDB version
7.0.8
Operating system
Linux
Operating system version (i.e. 20.04, 11.3, 10)
Ubuntu 22.04
Issue
The following code is only logging when an
insertion
event happens; however, delete and update events are not being sent in the "change" listener.Let me know if I'm doing something wrong.
The text was updated successfully, but these errors were encountered: