Hello,
I have been tasked with writing a BASH script that cleans out records from a MongoDB collection as well as other things. The script is a cleanup script that will run from a cron job nightly in order to prevent certain records from filling up disk space. Unfortunately for me, I'm a little new to MongoDB- so I am dealing with a bit of a learning curve here.
For the parts of this script that are relevant to my MongoDB question... The BASH script I have written gathers a bunch of UIDs from another source and writes out a mongoscript.js file with a bunch of remove statements like this:
db.events.remove( { "UID": "xxxxxxxxx" } )
db.events.remove( { "UID": "xxxxxxxxx" } )
db.events.remove( { "UID": "xxxxxxxxx" } )
db.events.remove( { "UID": "xxxxxxxxx" } )
Then the next part of the script, it runs the mongoscript.js in a one-liner using mongo:
mongo $MONGODB --host $MONGODB_HOST --port $MONGODB_PORT --username $MONGO_USER --password $MONGO_PASS mongoscript.js
This all works great and it removes all the records I am expecting to remove. But I'm not satisfied with whats going on here...
The problem I have is that I cannot error-check this because the db.<collection_name>.remove( { some json } ) method doesn't return anything. Even when I run these commands manually from the Mongo Shell, there are no return codes or returned json telling me how many records were deleted or if something failed, etc. Does anyone know how I can produce the same results but with some error checking so I can validate that these entries were in fact removed?
Many Thanks!
John