Posts

Showing posts with the label documentation

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 Tips: Views

use <db>; This command will give you all view definition under the db you are in.  db.getCollectionInfos({type:'view'}); ex: {                 "name" : "utilizationSubcategoryNumbersOMD",                 "type" : "view",                 "options" : {                         "viewOn" : "utilizationSubcategoryNumbers",                         "pipeline" : [                                 {                                         "$match" : {                           ...