-
Notifications
You must be signed in to change notification settings - Fork 1
/
stream.py
36 lines (23 loc) · 868 Bytes
/
stream.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from connect import *
# Define the change stream pipeline to exclude documents with {'watcher': 'watched'}
pipeline = [
{"$match": {
"fullDocument.watcher": {"$ne": "watched"},
"operationType": {"$ne": "delete"}
}
}
]
# Watch the collection for changes with the specified filter
change_stream = collection.watch(pipeline=pipeline, full_document='updateLookup')
print("Watching for changes...")
collection.update_many({"watcher":{"$exists":False}},{"$set":{"watcher":"ready"}})
try:
for change in change_stream:
print('='*60,'\n',change,'\n','='*60)
print(type(change))
collection.update_one(
{'_id': change['documentKey']['_id']},
{'$set': {'watcher': 'watched'}}
)
except KeyboardInterrupt:
print("\n\n\t\tfiles watcher has been stopped.")