Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
MongoDB get estimate oplog per collection with profiler
// https://docs.mongodb.com/manual/tutorial/manage-the-database-profiler/#enable-and-configure-database-profiling
(() => {
return db.system.profile.aggregate([
{ $match: { $or: [{op: {$in: ['update', 'insert', 'delete']}}, {'command.writeConcern': {$exists: true}}] } },
{ $group: {_id: {op : '$op', ns: '$ns', client: "$client"}, count: {$sum: 1}, start: {$min: "$ts"}, end: {$max: "$ts"} } },
{ $match: { count: { $gt: 50}}},
{ $sort: {count: -1} }
]).map((requestStats) => {
let stats = db.getCollection(requestStats._id.ns.split('.')[1]).stats();
requestStats.queryPerHour = ( 3600000 / (requestStats.end - requestStats.start) ) * requestStats.count;
requestStats.estimateOpLogPerHour = stats.avgObjSize * requestStats.queryPerHour;
requestStats.estimateOpLogPerHourMB = requestStats.estimateOpLogPerHour / ( 1024 * 1024 );
return requestStats;
})
//.reduce((acc,e) => e.estimateOpLogPerHourMB + acc, 0);
.sort((a,b) => b.estimateOpLogPerHourMB - a.estimateOpLogPerHourMB);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.