1

I want to know does there is any way in which i can delete mongo collections based on number of records i.e when count is 0 delete the collections using shell script

I have tried this

     export name=$(mongo test --eval "printjson(db['42QVM_20170701'].count())") 
     echo $name

but here it return string value with count i.e MongoDB shell version: 3.0.15 connecting to: test 0 i want to know does there is a way where i can automatically drop a collections based on size or count value `

gaurav singh
  • 1,376
  • 1
  • 15
  • 25
  • 1
    Why not just `if ( db['42QVM_20170701'].count() === 0 ) db['42QVM_20170701'].drop()`? It is a JavaScript REPL after-all. Also you should use `--quiet` to make your scripts less "noisy". – Neil Lunn Jul 19 '17 at 10:55
  • @NeilLunn but here i have to provide collection name i.e 42QVM_20170701 ....is there way were it can read all collections and find count then drop..coz i have 5k collections and giving collections name in this way would be time consuming – gaurav singh Jul 19 '17 at 11:34
  • 1
    You mean like ["Loop through all Mongo collections and execute query"](https://stackoverflow.com/a/25543598/2313887) – Neil Lunn Jul 19 '17 at 11:52
  • @NeilLunn yes.. – gaurav singh Jul 19 '17 at 14:01
  • Then considering you looked at it and found the information useful perhaps you should vote it up to indicate your approval of the usefulness to the community. Since it actually is an answer of mine, I notice that despite giving you the link, no upvote has been recorded there since. – Neil Lunn Jul 19 '17 at 21:58
  • @NeilLunn ....thank you ..i have upvoted your ans – gaurav singh Jul 20 '17 at 06:58

0 Answers0