Change Stream throwing Error "Unauthorised to execute command" with MongoDb Atlas

Hi,

We have recently migrated our data from Azure Cosmos to MongoDb Atlas. I have a node.js server that watches collections. This code was working perfectly with Azure Cosmos but is throwing the following error when switching out the URL to MongoDB Atlas:

const collection = db.collection(collectionName);
const changeStream = collection.watch([pipeline], { fullDocument: "updateLookup" });
 //listening to changes on the collection with roomId
 changeStream.on('change', (change) => { ... }

Cluster details:
VERSION 6.0.13
CLUSTER TIER M50 (NVMe SSD)
TYPE Replica Set - 3 nodes

connection URL format: “mongodb+srv://user:pass@.mongodb.net/dbName/?&retryWrites=true&w=majority”

I have also granted nearly all permissions on the db (all collections) including find and changeStream from the MongoDB Atlas console.

The full error:

MongoError: not authorized on {dbName} to execute command { aggregate: "system.views", pipeline: [ { $changeStream: { fullDocument: "updateLookup" } }, { $match: { operationType: { $in: [ "insert", "update", "replace" ] } } }, { $project: { _id: 1, fullDocument: 1, ns: 1, documentKey: 1 } } ], cursor: {}, lsid: { id: UUID("") }, $clusterTime: { clusterTime: Timestamp(1707836562, 4), } }, $db: "dbName" }

I have already tried

  • updating and downgrading the node.js driver
  • changing the URL connection string format to other variants suggested on Stack Overflow.

Would really appreciate any kind of help on how to even troubleshoot this!!!

Hello. Based on the error, my best guess is that you are trying to open a change stream on a ‘system.views’ collection, and it is not supported as a source for a change stream in MongoDB.

You can open a change stream cursor for a single collection (except system collections, or any collections in the admin , local , and config databases)

If you are looking to track events like creation of the view, you can use database-level change stream with option {showExpandedEvents:true} . It will include events like create/modify describing the creation and modification of collections, including views.

Hi Katya,

Thanks for the response! I am actually creating the change stream for a regular collection.

hm. Can you share a snippet where you are setting the collectionName?