Skip to content

Instantly share code, notes, and snippets.

@blowhacker
Created March 14, 2016 11:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blowhacker/c930142e967d3a123519 to your computer and use it in GitHub Desktop.
Save blowhacker/c930142e967d3a123519 to your computer and use it in GitHub Desktop.
mongo shell db.grep()
DBCollection.prototype.fields = function fields(rowdepth) {
var fields = {};
if (typeof rowdepth == 'undefined')
depth = 101 * 2;
this.find().limit(rowdepth).sort({
_id: -1
}).forEach(function(x) {
var rowfields = dotNotateFields(x);
for (var i in rowfields) {
fields[i] = rowfields[i];
}
})
var ret = [];
for(var i in fields){
ret.push(i);
}
return ret;
function dotNotateFields(obj, prefix) {
var fields = {};
if (!prefix) prefix = "";
for (var j in obj) {
if (isNaN(parseInt(j))) fields[prefix + j] = 1;
var x = Object.prototype.toString.call(obj[j]);
if ((x.match(/bson_object/) || x.match(/Array/) || x.match(/object Object/)) && !x.match(/ObjectId/)) {
var px = isNaN(parseInt(j)) ? j + '.' : '';
var fieldsdot = dotNotateFields(obj[j], px);
for (var i in fieldsdot) {
fields[prefix + i] = 1;
}
}
}
return fields;
}
}
//requires DBCollection.prototype.fields
DB.prototype.grep = function(search, includeNonIndexedFields) {
var collections = this.getCollectionNames();
var ret = [];
if(!includeNonIndexedFields)
includeNonIndexedFields = false;
for (var i = 0; i < collections.length; i++) {
var col = collections[i];
var indexes = this.getCollection(col).getIndexKeys();
var findFields = {};
if (includeNonIndexedFields) {
var findFieldsArray = this.getCollection(col).fields();
for(var c =0;c<findFieldsArray.length;c++){
findFields[findFieldsArray[c]] = 1;
}
} else {
for (var j = 0; j < indexes.length; j++) {
for (var field in indexes[j]) {
findFields[field] = 1;
break;
}
}
}
for (var field in findFields) {
var farr = [];
farr[field] = search;
if (this.getCollection(col).findOne(farr)) {
print(search + " exists in " + this + "." + col + "." + field);
ret.push({
collection: col,
field: field
});
}
}
}
return {
search: search,
includeNonIndexedFields: includeNonIndexedFields,
results: ret
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment