Search code examples
mongodbpymongochangestream

How to watch for changes for all collections in a database with pymongo


I am trying to watch for realtime changes in all of the collections in a MongoDB database. I am following pymongo docs at https://pymongo.readthedocs.io/en/stable/api/pymongo/database.html#pymongo.database.Database.watch but it seems to only work for individual collection not the whole database:

import pymongo 

uri = 'mongodb://localhost:27017/myDBName'  
db = client[myDBName]  
client = pymongo.MongoClient(uri)

    try:
            with db.watch(
                    [{'$match': {'operationType': 'insert'}}]) as stream:
                for insert_change in stream:
                    print(insert_change)
            
    except Exception as e:
            print('OOPS!! Some ERROR Occurred')
            print(e)

but I get the following error:

{aggregate: 1} is not valid for '$changeStream'; a collection is required., full error: {'operationTime': Timestamp(1614185450, 3), 'ok': 0.0, 'errmsg': "{aggregate: 1} is not valid for '$changeStream'; a collection is required.", 'code': 73, 'codeName': 'InvalidNamespace', '$clusterTime': {'clusterTime': Timestamp(1614185450, 3), 'signature': {'hash': b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', 'keyId': 0}}}

when I change it to db.myCollectionName.watch()... it is working however. What am I doing wrong? I tried using a loop through all collections and watch for each, but that only watches for the first collection in database and not entire range of for collection in collections:


Solution

  • You need to use a new enough MongoDB server that supports this functionality.