Useful Commands
Redis
Clear key cache
redis-cli -p 6378 -a 'password' --raw keys 'lr:tok:*' | xargs redis-cli -p 6378 -a 'password' del
Mongo
Delete unit test DBs
db.adminCommand("listDatabases").databases.forEach( function (d) {
if (d.name.includes("test_")) {
print(d.name);
db.getSiblingDB(d.name).dropDatabase();
}
})
mongo --eval 'db.adminCommand("listDatabases").databases.forEach( function (d) {
if (d.name.includes("test_")) {
print(d.name);
db.getSiblingDB(d.name).dropDatabase();
}
})'
Find and remove index of unknown location
db.getCollectionNames().forEach(coll =>
{
const col_obj = db.getCollection(coll);
indexes = col_obj.getIndexes();
let found = indexes.filter(i=> i.name === 'lastUpdated_1');
if (found.length > 0) {
print("Found on " + coll + ":");
printjson(found);
found.forEach(f => col_obj.dropIndex(f.name)); //drop the index
}
});
Mongo Query Explains
function quick_explain(explainPlan) {
var stepNo = 1;
var printInputStage = function(step) {
if ("inputStage" in step) {
printInputStage(step.inputStage);
}
if ("inputStages" in step) {
step.inputStages.forEach(function(inputStage){
printInputStage(inputStage);
});
}
if ("indexName" in step) {
print(stepNo++, step.stage, step.indexName);
} else {
print(stepNo++, step.stage);
}
};
printInputStage(explainPlan);
}
function executionStats(execStats) {
var stepNo = 1;
print('\n');
var printSpaces = function(n) {
var s = '';
for (var i = 1; i < n; i++) {
s += ' ';
}
return s;
};
var printInputStage = function(step, depth) {
if ('inputStage' in step) {
printInputStage(step.inputStage, depth + 1);
}
if ('inputStages' in step) {
step.inputStages.forEach(function(inputStage) {
printInputStage(inputStage, depth + 1);
});
}
var extraData = '(';
if ('indexName' in step) extraData += ' ' + step.indexName;
if ('executionTimeMillisEstimate' in step) {
extraData += ' ms:' + step.executionTimeMillisEstimate;
}
if ('keysExamined' in step)
extraData += ' keys:' + step.keysExamined;
if ('docsExamined' in step)
extraData += ' docs:' + step.docsExamined;
extraData += ')';
print(stepNo++, printSpaces(depth), step.stage, extraData);
};
printInputStage(execStats.executionStages, 1);
print(
'\nTotals: ms:',
execStats.executionTimeMillis,
' keys:',
execStats.totalKeysExamined,
' Docs:',
execStats.totalDocsExamined
);
}
Power delete current directory
# Better option with `find` exists below
# commands are dangerous!
fd -tf --threads=32 --exec rm {}
find . -delete
for i in {0..520}
do
echo "Deleting $i dir"
fd -tf . "$i/" --threads=32 --exec rm {}
done
Mongo Backup/Restore
mongoexport --uri 'mongodb://localhost' --db main --collection targetCollection --out PatchToApply.json
mongoimport --numInsertionWorkers=32 --mode=upsert --uri 'mongodb://localhost' --db main --collection targetCollection PatchToApply.json
mongoimport --numInsertionWorkers=32 --mode=upsert --uri 'mongodb://localhost' --db main --collection targetCollection --mode=upsert --upsertFields=f1,f2,f3,key NoIdNoMongoPatchToApply.json
mongoimport --numInsertionWorkers=32 --mode=upsert --uri 'mongodb://localhost' --db main --collection targetCollection PatchToApply.json
Monitor metrics endpoint
netcat -u -l -p 8125 localhost
Caddy local https
{
local_certs
debug
}
https://127.0.0.1:5001 {
reverse_proxy {
to http://127.0.0.1:5000
#tls_insecure_skip_verify
#transport http {
# tls internal
#tls_insecure_skip_verify
#}
}
}
caddy run --config http.caddyfile --adapter caddyfile
Kill application on Port
kill -9 $(lsof -ti:3000)
Git
unset/reset credential helper (windows)
git config --system --unset credential.helper
git config --system credential.helper manager
rewrite project author history (!danger!)
git rebase -r --root --exec "git commit --amend --no-edit --reset-author"
Add secondary push Remote
git remote set-url origin --push --add <a remote>
git remote set-url origin --push --add <another remote>