V
V
vjufvufcgyf2020-11-07 03:21:39
MongoDB
vjufvufcgyf, 2020-11-07 03:21:39

How to know in mongodb that certain data has changed?

How to know in mongodb that certain data has changed?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nowm, 2020-11-07
@vjufvufcgyf

In mongo, since version 3.6. there is such a thing as Change Streams . The bottom line is that you in the code, after connecting to the database, subscribe to data changes in a special way. For example, if you want to track changes in the Books collection:

const collection = db.collection('Books');
const changeStream = collection.watch();
changeStream.on('change', event => {
  // event — см. типы событий: https://docs.mongodb.com/manual/reference/change-events/
});

An important point: Change Streams only works if the MongoDB server is running in cluster mode, not how it starts by default. A few details on how to do this: https://habr.com/en/post/335772/ (see the "Configuring and adding servers to the Replica Set" section).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question