Cannot execute the compact command : command not allowed

Hello, I got this error MongoServerError[AtlasError]: CMD_NOT_ALLOWED: compact when trying to compact a collection from mongosh. I connect using a user with atlasAdmin @admin role and with an additional custom role that I created specifically for the compact operation on that collection. The database version is 8. The compact command that I use is

db.runCommand(
   {
     compact: "(my collection name)",
     force:true	
   }
)

I got this error only if I connect to a database hosted on Atlas but it works when I connect to a local database.
The user has been created from the Atlas UI using the button “Add New Database User” and with this user I can read and write all the collections, but the compact command is not allowed.

This is expected. MongoDB Atlas has a list of limitations and this command is not supported on shared tiers(M0,M2,M5)

2 Likes

Adding something to Chris’s statement, I would just say that it is necessary to access each node using port 27017 directly, so that it can be executed specifically on the node. I have a Python script that I made, with my requirements, but that you could adjust as needed, to perform compaction and failover when it is a primary. It works for both shard and replicaset. If you want it, contact me and I will send it to you.

1 Like