How to know data has been newly added in your MongoDB?

I have an nodejs server running witch show data on a web interface. The data is fetched from a MongoDB using mongoose. The data is added via an node-red application witch is isolated from the rest.

Currently my nodejs server fetches the data every 5 seconds. Is there a way to know if the data in my MongoDB has changed?

Thanks, I hope my question is clear.

mygiftcardsite

Hello @Leon_Rodriguez, welcome to the MongoDB Community forum!

There are different ways to check if data is being inserted into a MongoDB collection. It depends upon what kind of information you are looking for and how you want to use it. Here are some ways:

Query

You can simply query your collection and count the documents to know if new documents are added or deleted. This may not give information about data updates. db.collection.countDocuments() runs from mongo shell. Querying periodically, you will know that data is added by reviewing the numbers.

Change Streams

You can use Change Streams to track the data inserted into a collection in real time. Change Streams functionality is available with Replica-set and sharded clusters (not on Standalone).

Command-line tools

You can also use a command-line tool like mongotop to interactively watch that the write operations are happening on a specific collection.

Another way is to query the server log files, to track the writes to a specific collection. An useful tool like mtools can be used to query log details about specific collection, operation and time.