Automattic / monk

The wise MongoDB API
MIT License
1.85k stars 182 forks source link

Collection Watch - Change Streams #271

Open mustafa-kahraman opened 5 years ago

mustafa-kahraman commented 5 years ago

Hi all,

How can I watch Change Streams with monk?

const MongoClient = require("mongodb").MongoClient;
const assert = require("assert");
/*
Modify Change Stream Output using Aggregation Pipelines
You can control change stream output by providing an array of one or more of the following pipeline stages when configuring the change stream:
$match, $project, $addFields, $replaceRoot, $redact
See Change Events for more information on the change stream response document format.
*/
const pipeline = [
  {
    $project: { documentKey: false }
  }
];
MongoClient.connect("mongodb://localhost:27017,localhost:27018,localhost:27019?replicaSet=mongo-repl")
  .then(client => {
    console.log("Connected correctly to server");
    // specify db and collections
    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);
    });

  })
  .catch(err => {
    console.error(err);
});
garzj commented 3 years ago

It would at least be nice to have the ability to access the underlying native mongodb connection.