Posts

Showing posts with the label Script

MongoDB Script Get all Indexes Creation Script

MongoDB: Get All Indexes in DB via Mongo Shell This is a simple script to backup all the index creation scripts Note: background:true is deprecated since MongoDB 4.2, but it's okay to leave it in. Options for all Index types To run this, simply save to a script and run it with mongo shell mongo -u admin -p --authenticationDatabase=admin --host localhost getAllIndexesCreationScript.js > output.js db.getMongo().getDBNames().forEach(function(dbName) { if (dbName != "admin" && dbName != "local") { db.getSiblingDB(dbName).getCollectionNames().forEach(function(coll) { db.getSiblingDB(dbName)[coll].getIndexes().forEach(function(index) { if ("_id_" !== index.name) { print("db.getSiblingDB('" + dbName + "')." + coll + ".createIndex(" + tojson(index.key) + ",{background:true}" + ")"); } }); }); } });

MongoDB Log Switch Script

Image
It's been a while since I wrote a post, I have been busy exploring everything I want to learn outside of work and it's been fun. I think it's time to get back and document some more!  Today I want to talk about  log switching.  Having been managing MongoDB databases and MongoDB BI Connector for quite a while now. I can't put enough appreciation of how the logs have helped of making my life so much easier and they should be the one of the first things we check whenever we encounter any issue in regards to the aforementioned.  If you agree with me on how important the log is, please read on. Log is definitely your friend, but I know I had made the mistake of leaving it alone for a long while and when I truly needed it, it became a monster that I had to cut and dissect into workable size.  There was one time we had a production issue and I had to look into the issue immediately and of course I went straight to my friend, the logs. I tried looking into the log and t...

MongoDB Tips: Kill long running processes

This is just a quick and simple script to find all active opid that has been running over 10 seconds and they are running against the databases and collections you can specify in the "ns" filter. It's best to first run db.currentOp(    {      "active" : true,      "secs_running" : { "$gt" : 10 },      "ns" : {$in:["equipment.detachment","personnel.detail","personnel.unavailability","personnel.detachment"]}    } ).inprog.forEach(function (doc) { print(doc.opid); }) and verify your output before you execute the kill command in my experience to make sure you are killing the processes that are meant to be killed. Once verifed, simply run: db.currentOp(    {      "active" : true,      "secs_running" : { "$gt" : 10 },      "ns" : {$in:["<db1>.<col1>","<db2>.<col2>","<dbn>.<coln>...