Node.js listen to MongoDB change
Asked Answered
R

4

5

Is there a way for Node.js to listen to a change in a particular data in a collection of MongoDB, and fire an event if a change happens?

Raneeraney answered 28/9, 2011 at 18:27 Comment(2)
Where are you writing to the MongoDB store from? From only the node.js instance, or from multiple sources?Maid
Well.. mongo is connected to node.js however, people can manually change the data in mongo directly. (such as mongoHub)Raneeraney
R
2

MongoDB apparently now supports triggers and watch, this answer is outdated.

[Original] I believe you are looking for a database trigger.

Unfortunately, MongoDB has no support for them yet, so I don't think you can listen for changes directly from the database. You'll need to setup some sort of notification system (e.g. pub/sub) that alerts interested parties when a collection has changed.

Rabah answered 28/9, 2011 at 22:26 Comment(4)
really hope triggers become available. They talked about them at the last MongoSFInterleaf
There are triggers in mongodb stitchTrager
MongoDB supports .watch() method.Timberhead
Stackoverflow needs a way to "unaccept" your own accepted answer so that when things change better answers float to the top...Rabah
R
14

Well, this is an old question, but I was struggling with the same thing. I found a number of tidbits that helped me put together a solution, and I've published it as a library:

https://github.com/TorchlightSoftware/mongo-watch

The library is written in coffeescript. Here's an example in javascript, for those that prefer.

var MongoWatch = require('mongo-watch'),
    watcher = new MongoWatch({parser: 'pretty'});

watcher.watch('test.users', function(event) {
  return console.log('something changed:', event);
});
Revest answered 6/3, 2013 at 8:49 Comment(1)
Can you share more about the structure of the returned event from watcher.watch(...)Lackluster
R
2

MongoDB apparently now supports triggers and watch, this answer is outdated.

[Original] I believe you are looking for a database trigger.

Unfortunately, MongoDB has no support for them yet, so I don't think you can listen for changes directly from the database. You'll need to setup some sort of notification system (e.g. pub/sub) that alerts interested parties when a collection has changed.

Rabah answered 28/9, 2011 at 22:26 Comment(4)
really hope triggers become available. They talked about them at the last MongoSFInterleaf
There are triggers in mongodb stitchTrager
MongoDB supports .watch() method.Timberhead
Stackoverflow needs a way to "unaccept" your own accepted answer so that when things change better answers float to the top...Rabah
T
2

MongoDB 3.6 introduced change streams, which are designed to solve precisely this problem.

Here's an example: https://github.com/thakkaryash94/mongodb-change-streams-nodejs-example

const pipeline = [
  {
    $project: { documentKey: false }
  }
];

const db = client.db("superheroesdb");
const collection = db.collection("superheroes");

const changeStream = collection.watch(pipeline);
// start listen to changes
changeStream.on("change", function (change) {
  console.log(change);
});
Trager answered 28/6, 2019 at 14:42 Comment(0)
J
1

Well, kind of late.
But still, if someone looking to notify the MongoDB changes to the node.js then they can use mubsub library.

This is active library and very easy to integrate with nodejs. It uses Mongo's tailable cursors and capped collections.
It works in pub/sub fashion to notify the subscriber when the document inserted into the MongoDB.

Check on Github for details.

Jamilajamill answered 4/3, 2017 at 22:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.