Last active
April 12, 2023 08:00
-
-
Save alkampfergit/1fe72777f3455ca2c28064c9ea9f0753 to your computer and use it in GitHub Desktop.
Complete profile of mongodb databases
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongo = db.getMongo() | |
var dbs = db.getMongo().getDBNames() | |
//Step 1 drop all existing profile and recreate profile with bigger collections. | |
for(var i in dbs){ | |
db = db.getMongo().getDB( dbs[i] ); | |
var name = db.getName(); | |
if (name.startsWith("jarvis")) | |
{ | |
db.setProfilingLevel(0) | |
db.system.profile.drop() | |
db.createCollection( "system.profile", { capped: true, size:40000000 } ) | |
db.setProfilingLevel(1, 0) | |
} | |
} | |
//this code can be run periodically to copy all profiling into a single unique collection | |
var targetDBName = "profile"; // Change this to the name of the target database | |
var targetCollectionName = "systemprofile"; // Change this to the name of the target collection | |
mongo.getDB(targetDBName).getCollection(targetCollectionName).drop() | |
//now iterate over all database and copy data into another collection | |
for(var i in dbs){ | |
db = db.getMongo().getDB( dbs[i] ); | |
var name = db.getName(); | |
if (name.startsWith("jarvis")) | |
{ | |
print ('copy data of: ' + name); | |
db.getCollection('system.profile').aggregate([ | |
{ | |
$merge: { | |
into: { | |
db: targetDBName, | |
coll: targetCollectionName | |
}, | |
whenMatched: "replace", // You can choose "replace", "keepExisting", or "fail" if you want a different behavior when documents with the same _id exist in the target collection | |
whenNotMatched: "insert" | |
} | |
} | |
]); | |
print ('FINISHED: copy data of: ' + name); | |
} | |
} | |
//then perform some aggregation to verify which collection is more impacted. | |
db.getCollection('systemprofile').aggregate([ | |
{$match : {"command.aggregate" : {$ne : "system.profile"}}}, | |
{$project : {signature : {$concat: ["$op", '/', "$ns"]}}}, | |
{$group : {_id : "$signature", Sum : {$sum : 1}}}, | |
{$sort : {Sum : -1}} | |
]) | |
//run this code to drop everything. | |
for(var i in dbs){ | |
db = db.getMongo().getDB( dbs[i] ); | |
var name = db.getName(); | |
if (name.startsWith("jarvis")) | |
{ | |
db.setProfilingLevel(0) | |
db.system.profile.drop() | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment