Why change streams cannot be used with local database?

When I try to use change streams with local database, I get below error. Why is it not allowed?

    test 1:SECONDARY> watchCursor = db.getSiblingDB("local").oplog.rs.watch()
2020-04-21T12:24:22.502+0530 E QUERY    [js] Error: command failed: {
        "operationTime" : Timestamp(1587452057, 1),
        "ok" : 0,
        "errmsg" : "$changeStream may not be opened on the internal local database",
        "code" : 73,
        "codeName" : "InvalidNamespace",
        "$clusterTime" : {
                "clusterTime" : Timestamp(1587452057, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }

Hi Akshaya,

Change streams can’t be opened on the local database because it is an internal system database (as per the error message). You cannot open change stream cursors on the local, admin, or config system databases (or any collections in those databases).

It looks like you are trying to open a change stream on the oplog.rs collection, which is the underlying collection used for change stream events.

When you use the Change Stream API, you open change streams at a collection, database, or deployment level. The supported equivalent for what you are trying to do would be watching all changes for the deployment via Mongo.watch() in the mongo shell. You can also perform this operation from a supported MongoDB driver.

Regards,
Stennie

2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.