Compact a Collection

I deleted a significant amount of data from a single collection and would like to reclaim the space (including the index space). I successfully ran compact on that collection for the primary. I then did a stepdown and ran it again so that the other node was also compacted. However, I’m not able to run compact on the third node. The priority of this node in Atlas is 6.5 (vs 7.0) for the others so when I run a stepdown it doesn’t become primary. My question therefore is how can I complete this compact? I’m not sure how to connect just to this single non-primary node to run it there as I keep connecting just to the primary. And MongoDB Atlas keeps giving me permission errors when I try to change the priority to force this third node to be the primary. So, right now I’m stuck with a larger database than I need because of this issue and would like to fix that.

Hi @Andrew_Pitts ,

Welcome to MongoDB community.

Actually you should avoid using primary compact and run it on secondary.

So what I would suggest is to go to the connect tab and use an older connection string format where all hosts are present and place only the specific node in the string (non srv).

This will directly connected to the secondary .

Than compact on it.



@Pavel_Duchovny Thank you! The suggestion to use the older connection string format was what I needed and worked like a charm. I was able to connect to the secondary in that way.

For others that may come across this, please note that I needed to remove the replicaSet= part of that string and also that I needed to run rs.secondaryOk() before I could operate on the secondary.


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