Update document during changeStream event

Hi, according with Change Stream documentation

There is an example

const pipeline = [
  { $match: { 'fullDocument.username': 'alice' } },
  { $addFields: { newField: 'this is an added field!' } }
];
const collection = db.collection('inventory');
const changeStream = collection.watch(pipeline);
changeStream.on('change', next => {
  // process next document
});

This means that the document will be modified and a new saved attribute newField will be added right?

I performed this operation, and it didn’t happen, I capture the event, I added a field , I even needed to add it inside fullDocument.

i put log, and the new attribute appears in the log, but it does not persist on database.

   const pipeline = [
      {
        $match: { operationType: { $in: ['insert'] } },
      },
      {
        $addFields: { fullDocument: { createdAt: new Date(), handled: true } },
      },
      { $project: { fullDocument: 1 } },
    ];```


Is there something wrong, or did I get it wrong?

Thanks in advance.

Hello @navarro_ferreira ,

I notice you haven’t had a response to this topic yet - were you able to find a solution?
If not, could you confirm if my understanding for your use-case is correct?

By using changeStream event, you are trying to insert a new field: value back in the original collection that matches your $match criteria. If this is correct, then I’m afraid changestream does not have this feature yet. From the changestream documentation page:

Change streams allow applications to access real-time data changes without the complexity and risk of tailing the oplog. Applications can use change streams to subscribe to all data changes on a single collection, a database, or an entire deployment, and immediately react to them. Because change streams use the aggregation framework, applications can also filter for specific changes or transform the notifications at will.

The pipeline in the changestream only allows you to modify the changestream output from the default output but it can not update the data in database. This is so that if you’re only interested in part of a document in the changestream (e.g. if you only need to know the _id of the changed document), the changestream doesn’t need to return the whole document and instead project only the _id using the defined pipeline.

However, should you need to persist the changestream output, you might need to do it manually by modifying the code example you posted as below:

Let me know if you have any more questions.

Regards,
Tarun

1 Like