Last active
May 22, 2019 08:38
-
-
Save rvanmil/db0735e6a8e91039cc73a5ece0ddfd12 to your computer and use it in GitHub Desktop.
parse mongodb oplog
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
const fs = require('fs') | |
fs.readFile('./oplog.dump', 'utf-8', (err, jsonString) => { | |
const oplog = JSON.parse(jsonString) | |
const operationsMap = new Map() | |
oplog.forEach((oplogItem) => { | |
const { ns, op } = oplogItem | |
let operation = operationsMap.get(ns) | |
if (operation) { | |
if (op === 'd') { | |
operation.delete += 1 | |
} | |
if (op === 'i') { | |
operation.insert += 1 | |
} | |
if (op === 'u') { | |
operation.update += 1 | |
} | |
operation.total += 1 | |
} else { | |
operation = { | |
collection: ns, | |
delete: (op === 'd') ? 1 : 0, | |
insert: (op === 'i') ? 1 : 0, | |
update: (op === 'u') ? 1 : 0, | |
total: 1 | |
} | |
} | |
operationsMap.set(ns, operation) | |
}) | |
const operations = [...operationsMap.values()] | |
operations.sort((a, b) => b.total - a.total) | |
const operationsJsonString = JSON.stringify(operations) | |
fs.writeFile('./operations.json', operationsJsonString, (err) => { | |
if (err) { | |
console.log('Fail') | |
} else { | |
console.log('Done') | |
} | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
mongoexport -h dbhost -d local -c oplog.rs -u adminuser -p password -o oplog.dump --authenticationDatabase=admin