How to work with changestreams for a collection?

Hi Team,

We want to implement changestreams for one of the collection and we have written the below code:

cat schema_creation_cs.sh
#!/bin/bash
/home/mongodb/MONGODB_TESTDB/mongodb/mongodb/bin/mongo --host wikloh.hp.internet --port 27000 --authenticationDatabase=admin -u mongodb -p XXXXX < /home/mongodb/change_stream/change_stream.json >> /home/mongodb/change_stream/schema_creation_cs.log

cat change_stream.json

use KLP_MGMT_19
console.log("Watching for new changes  in this collection 'TestCounter' ...");

db.TestCounter.watch([{
    $match: {
        //operationType: "update", //update|insert|delete|replace|invalidate
    }
    }],
    {
        fullDocument: "updateLookup", //default|updateLookup
    })
    .on("change", (data) => {
        console.log(tojson(data))
    })
    .on("error", (err)=>{
        console.error(err)
   });

while (true) {
sleep(1000)
}

The code works well in nosqlbooster tool but same when i try to execute from server level getting below error:

switched to db KLP_MGMT_19
2020-09-03T06:00:52.217+0000 E QUERY    [js] ReferenceError: console is not defined :
@(shell):1:1
2020-09-03T06:00:54.152+0000 I CONTROL  [main] shutting down with code:0

Please let me know hw to run the same via server and i also want to run this in background using nohup?

Regards

Hi @Mamatha_M

The error is because the mongo shell exposes a different API than the node driver. For example, console.log() is not a method the shell recognizes, as signified by the error message.

See db.collection.watch() for the API exposed by the shell with regard to change stream.

Roughly, what you want could be translated as such in the mongo shell:

cur = db.test.watch([], {fullDocument:'updateLookup'})
while(!cur.isExhausted()) {
  if (cur.hasNext()) {
    printjson(cur.next())
  }
}

Best regards,
Kevin

Hi Kevin,

Thanks for the information.

I have heard MongoDB Kafka Connector also helps in changestreams to capture the CURD operation.

We have community version of mongo installed in our environment, So my query is will MongoDB Kafka Connector work for community edition or will it work only for Enterprise version?

Is there any other method by which we can work on changestreams?

Regards
Mamatha