Skip to content

Instantly share code, notes, and snippets.

View normgraham's full-sized avatar

Norman Graham normgraham

View GitHub Profile
@normgraham
normgraham / listUsers.js
Last active August 29, 2015 14:07
Print documents from system.users collections in all databases
var listUsers = function () {
db.adminCommand({"listDatabases":1}).databases.forEach(function(doc) {
print(doc.name + ":");
db.getSiblingDB(doc.name).getCollection("system.users").find().forEach(printjson);
});
}
@normgraham
normgraham / convertTypes.js
Created March 6, 2014 17:46
Copies documents from one collection to another, converting the types of field values along the way. This is a little hacky and isn't the kind of thing you'd normally want to do in JavaScript, but if you have no other convenient choices this script is a good example of the kinds of things you'll need to check. Invoke with something like {code} m…
var convertTypes = function (sourceNS, targetNS) {
var stringProps = ["field1", "field2", "field3", "field4"];
var numberIntProps = ["field5", "field6"];
var dateProps = ["field7"];
source = db.getSiblingDB(sourceNS.split(".")[0])[sourceNS.split(".")[1]];
target = db.getSiblingDB(targetNS.split(".")[0])[targetNS.split(".")[1]];
var convertToString = function (prop) {
if (typeof prop === 'string')
@normgraham
normgraham / checkAllVersions.js
Last active February 12, 2016 02:41
This script traverses all the hosts in a sharded cluster; collects cmdLineOpts, buildInfo, hostInfo, and serverStatus, and makes some basic checks. To use it, save the script on one of your machines and execute it with the following command, ensuring that $HOST and $PORT specify one of your mongos processes: {code} mongo --host $HOST --port $PO…
var checkAllVersions = function (verbose) {
var equalOrLaterVersion = function (v1, v2) {
return (v1[0] > v2[0]) ||
(v1[0] == v2[0] && v1[1] > v2[1]) ||
(v1[0] == v2[0] && v1[1] == v2[1] && v1[2] >= v2[2]);
}
var getHostInfo = function (info) {
try {
@normgraham
normgraham / copySystemProfile.sh
Last active August 29, 2015 13:55
Copy a system.profile collection in a database by dumping it and then restoring it to a new database.collection.
#!/usr/bin/env bash
# Edit the following variables appropriately
SOURCEDB="mydb"
TARGETDB="test"
TARGETCOLL="sp"
mongodump -d $SOURCEDB -c system.profile -o /tmp
mv /tmp/$SOURCEDB/system.profile.bson /tmp/$SOURCEDB/$TARGETCOLL.bson
mongorestore --drop -d $TARGETDB -c $TARGETCOLL /tmp/$SOURCEDB/$TARGETCOLL.bson
@normgraham
normgraham / tagQueries.js
Last active August 29, 2015 13:55
Scans a copy of the system.profile collection named sp and for each document that has a query field inserts a queryHash field that's a hash of the field names and orderby components of the query. Essentially creates a signature for the query. Includes a sample aggregation pipeline for analysis.
// Assuming that sp the name of collection that's a copy of the system.profile collection,
// the following code tags each document containing a query field with a queryHash value
// based on the shape of the query and orderby (or sort). There's also a sample aggregation
// pipeline that uses the new queryHash field. E.g. tagQueries("sp") or aggQueries("sp")
var tagQueries = function (sp) {
var queryToHash = function (doc) {
var props = new Array();
// Recursively walk the query or sort document pushing fieldnames onto the props array.
@normgraham
normgraham / fieldNameStats.js
Created January 30, 2014 19:41
Collect field name stats over all documents in a collection
map = function () {
totalDocs++;
var C = function(node, prefix) {
for (var inner_prop in node) {
if (typeof node[inner_prop] == 'object') {
emit(prefix + "." + inner_prop, 1);
result = C(node[inner_prop], prop + "." + inner_prop);
}
else if (node.hasOwnProperty(inner_prop) && inner_prop != 'str'){
emit(prefix + "." + inner_prop, 1);
@normgraham
normgraham / diffIndexes.js
Created January 30, 2014 19:39
Identify missing and non-matching indexes in a replica set
var diffIndexes = function() {
var replsetInfo = new Array();
var maxIterations = 0;
rs.slaveOk();
rs.status().members.forEach(function(member) {
if (member.stateStr == "PRIMARY" || member.stateStr == "SECONDARY") {
var memberInfo = {
name : member.name,
cursor : 0,
@normgraham
normgraham / printDupValidKeys.js
Created January 30, 2014 19:33
Prints a list of duplicate keys (here as the Value field in the StringLookup collection of all dbs) that are strings shorter than the maximum key length of 1024 taking overhead into account, which makes the maximum string key length to be 1011.
var printDupValidKeys = function() {
db.adminCommand({"listDatabases":1}).databases.forEach(
function(sdb) {
print(sdb.name);
try {
dupes=db.getSiblingDB(sdb.name).StringLookup.aggregate(
[ { $group: {
"_id" : "$Value",
"c": { "$sum" : 1 } } },
{ $match : {
@normgraham
normgraham / printAllIndexes.js
Created January 30, 2014 19:28
Print all the indexes on all the databases on a replica set member
var printAllIndexes = function() {
rs.slaveOk()
db.adminCommand({"listDatabases":1}).databases.forEach(function(doc){
print(doc.name);
db.getSiblingDB(doc.name).system.indexes.find().sort({ns:1,name:1}).forEach(printjson);
});
}
@normgraham
normgraham / validateCollections.js
Last active August 29, 2015 13:55
Validate all collections in a database
var validateCollections = function(dbname) {
var vdb = db.getMongo().getDB(dbname);
vdb.getCollectionNames().forEach(function(coll) {
printjson(vdb[coll].validate(true));
});
}