MongoDB Change Stream with Array Containing Element?

Viewed 453

I am trying to use a change stream in MongoDB to watch for changes in documents where the participants array contains a userID.

I understand MongoDB change streams allow a user to watch for changes on a document where an individual field matches; however, how can I watch for changes wherever the 'participants' array contains the userID?

 const pipeline = [
    {'$match': {'fullDocument.participants': socket.uid}}
]
const changeStream = Goal.watch(pipeline)
changeStream.on('change', (change) => {
    console.log(change); // You could parse out the needed info and send only that data. 
    socket.emit('changeData', change);
});

This code is treating participants as a single field and only watching where participants is equal to socket.uid; is it possible to watch where participants contains socket.uid?

0 Answers
Related