Skip to content

Instantly share code, notes, and snippets.

View karan9's full-sized avatar
🏠
Working from home

Karan Srivastava karan9

🏠
Working from home
View GitHub Profile
@karan9
karan9 / zstd.js
Created June 29, 2021 04:54
Create a collection with zstd compression
// Create a ZSTD Collection
db.createCollection("COLLNAME", {storageEngine: {wiredTiger: {configString: "block_compressor=zstd"}}})
// Check which block compressor is being used for a collection
var wt_options = db.zlogs.stats().wiredTiger.creationString.split(',')
// filter the options
wt_options.filter((wt_options) => wt_options.startsWith('block_compressor'))
@karan9
karan9 / updatedKeyhole.js
Last active February 16, 2021 03:37
Updated Version of Keyhole Script to get rid to index summary errors ..
// Copyright 2020 Kuei-chun Chen. All rights reserved.
var cluster = GetClustersSummary()
var data = JSON.stringify(cluster);
print(data);
function GetClustersSummary() {
var cluster = { "cluster": "standalone", "databases": [],
"keyhole": { "version": "v2.5.0-js", "collected": new Date(), "logs": [] } };
cluster["keyhole"]["logs"].push(new Date().toISOString()+" keyhole.js began");
var doc = db.serverStatus();
const MongoClient = require("mongodb").MongoClient;
const util = require('util')
const MONGOURI = "MONGOURL";
const DB = "lever-hire-nurture";
const COLL = "profiles";
const client = new MongoClient(MONGOURI, { useNewUrlParser: true, useUnifiedTopology: true });
client.connect(err => {
const { ClientEncryption } = require('mongodb-client-encryption');
const base64 = require('uuid-base64');
const mongoose = require('mongoose');
// DONOT USE IN PRODUCTION
function getEncryptionKey() {
const arr = [];
for (let i = 0; i < 96; ++i) {
arr.push(i);
}
@karan9
karan9 / formatLogFile.js
Last active December 1, 2020 19:43
Simple MongoDB Log Fixer For People who use windowwwwzzzz
const rl = require("readline");
const FILEPATH = "/Users/Karan.Srivastava/Desktop/Manish Sahni/s1.log";
const OUTPUTFILE = "s1k.log";
const lineReader = rl.createInterface({
input: require("fs").createReadStream(require("path").normalize(FILEPATH))
})
let prevLine = "";
var colls = db.getCollectionNames();
var ret = [];
colls.forEach(function(coll) {
if (String(coll).startsWith("system")) {
return;
}
var indexes = db.getCollection(coll).getIndexes();
var stats = db.getCollection(coll).aggregate( [ { $indexStats: { } } ] ).toArray();
var colls = db.getCollectionNames();
var ret = [];
colls.forEach(function(coll) {
if (String(coll).startsWith("system")) {
return;
}
var indexes = db.getCollection(coll).getIndexes();
var stats = db.getCollection(coll).aggregate( [ { $indexStats: { } } ] );
var objectIdFromDate = function (date) {
return Math.floor(date.getTime() / 1000).toString(16) + "0000000000000000";
};
var dateFromObjectId = function (objectId) {
return new Date(parseInt(objectId.substring(0, 8), 16) * 1000);
};
var colls = db.getCollectionNames();
var ret = [];
colls.forEach(function(coll) {
var indexes = db.getCollection(coll).getIndexes();
var len = indexes.length
ret.push({ "name": coll, "length": len });
})
printjson(ret)
@karan9
karan9 / hello_world_shell_local.js
Created October 26, 2020 10:56 — forked from kennwhite/hello_world_shell_local.js
MongoDB Client Side Field Level Encryption Quickstart Part 2 (local key version)
/*
Simple demonstration using MongoDB Client-Side Field Level Encryption (local key version)
Requires Community or (preferrably) Enterprise Shell and a MongoDB 4.2+ database
Local, stand-alone, or Atlas MongoDB will all work.
To use this, just open Mongo shell, with this file, e.g.:
mongo --nodb --shell hello_world_shell_local.js
# For a self-signed cert: mongo --tls --tlsCAFile /opt/mongodb/certs/ca.pem --shell hello_world_shell_local.js