3

MongoDB introduced change streams in their 3.6 release.
I wanted to implement mongo change stream in my code and wanted to understand how it works. I will implement using the java driver and it's pretty clear. But I wanted to know if there is there any way to open a change stream on in the mongo shell? Couldn't find much resources on that.

Vince Bowdren
  • 8,326
  • 3
  • 31
  • 56
Paras Diwan
  • 333
  • 1
  • 5
  • 13

1 Answers1

7

The db.collection.watch command opens a change stream cursor.

For example:

watchCursor = db.getSiblingDB("data").sensors.watch(
   [
      { $match : {"operationType" : "insert" } }
   ]
)

while (!watchCursor.isExhausted()){
   if (watchCursor.hasNext()){
      print(tojson(watchCursor.next()));
   }
}

Plenty more detail in the docs.

glytching
  • 44,936
  • 9
  • 114
  • 120
  • Good idea to make that more obvious. I have updated the answer to include this: `print(tojson(watchCursor.next()));`. – glytching Jan 17 '18 at 12:33