Skip to content

Instantly share code, notes, and snippets.

@rvanmil
Last active May 22, 2019 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rvanmil/db0735e6a8e91039cc73a5ece0ddfd12 to your computer and use it in GitHub Desktop.
Save rvanmil/db0735e6a8e91039cc73a5ece0ddfd12 to your computer and use it in GitHub Desktop.
parse mongodb oplog
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')
}
})
})
@rvanmil
Copy link
Author

rvanmil commented May 22, 2019

mongoexport -h dbhost -d local -c oplog.rs -u adminuser -p password -o oplog.dump --authenticationDatabase=admin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment