I am trying to set up a mongodb stream for a specific user but the thing is that you won’t be able to know what someone will give as a username, in this case the field is called: charactername.
For this project I am using: mongodb, express, nodejs and socket.io. The socket.io broadcasts it to the frontend and refreshed the page once there is a change in the database. But the thing is right now if something is changed in the collection “users” the refresh happens for every user instead of a specific user of whose document gets updated.
This is the code for the backend:
User.watch([{ $match: {
operationType:
{$in: ['update']},
"updateDescription.updatedFields.charactername": "" //What should I put here ?
}}]).
on('change', data => {
console.log('Update action triggered');
console.log(new Date(), data.updateDescription.updatedFields);
console.log(data)
io.emit("DBchange", data);
});
This is the code for the front end:
<script>
socket.on("DBchange", (data) => {
console.log(data)
location.reload()
});
</script>
How can I fix this issue?