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 / getStorageDataWithErrorCorrection.js
Last active February 5, 2021 11:22
A simple Mongo Shell function to get dataSize, storageSize and indexSize for each DB
function getStorageDetails() {
var db = this.db.getSiblingDB("admin");
var totalIndexSize = 0,
totalStorageSize = 0,
totalDataSize = 0,
formatSize = 1024 * 1024 * 1024;
// List the databases
var dbs = db.runCommand({ listDatabases: 1 }).databases;
@karan9
karan9 / getIndexDatabyBounds.js
Last active October 16, 2020 14:34
Returns number of indexes on each collection for a given database
var colls = db.getCollectionNames();
var ret = [];
colls.forEach(function(coll) {
var indexes = db.getCollection(coll).getIndexes();
var len = indexes.length
if (len >= 10) {
ret.push({ "name": coll, "length": len, "data": indexes});
}
})
function getStorageDetails() {
var db = this.db.getSiblingDB("admin");
var totalIndexSize = 0,
totalStorageSize = 0,
totalDataSize = 0,
formatSize = 1024 * 1024 * 1024;
// List the databases
var dbs = db.runCommand({ listDatabases: 1 }).databases;
@karan9
karan9 / getActiveConnectionsBySource.js
Created August 27, 2020 13:23
A simple mongo shell script to return the connections grouped by the source
db.currentOp(true).inprog.reduce(
(accumulator, connection) => {
ipaddress = connection.client ? connection.client.split(":")[0] : "Internal";
accumulator[ipaddress] = (accumulator[ipaddress] || 0) + 1;
accumulator["TOTAL_CONNECTION_COUNT"]++;
return accumulator;
},
{ TOTAL_CONNECTION_COUNT: 0 }
)
@karan9
karan9 / loadTest.js
Created September 8, 2020 06:45
Load testing script Atlas
const MongoClient = require("mongodb").MongoClient;
const { LoremIpsum } = require("lorem-ipsum");
const cluster = require('cluster');
const MONGOURI = "MONGO_URI";
const DB = "test_db";
const COLL = "test_coll";
/* Lorem Ipsum Generator */
var colls = db.getCollectionNames();
var ret = [];
colls.forEach(function(coll) {
var stats = db.getCollection(coll).stats();
ret.push({ "name": coll, "stats": stats});
})
printjson(ret)
function getStorageDetails() {
var db = this.db.getSiblingDB("admin");
var totalIndexSize = 0,
totalStorageSize = 0,
totalDataSize = 0,
formatSize = 1024 * 1024 * 1024;
// List the databases
var dbs = db.runCommand({ listDatabases: 1 });
@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
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)
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);
};