forked from TylerBrock/mongo-hacker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo_hacker.js
522 lines (434 loc) · 14.8 KB
/
mongo_hacker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/*
*
* Mongo Hacker
* MongoDB Shell Enhancements for Hackers
*
* Tyler J. Brock - 2013
*
* http://tylerbrock.github.com/mongo-hacker
*
*/
__ansi = {
csi: String.fromCharCode(0x1B) + '[',
reset: '0',
text_prop: 'm',
foreground: '3',
bright: '1',
underline: '4',
colors: {
red: '1',
green: '2',
yellow: '3',
blue: '4',
magenta: '5',
cyan: '6'
}
}
if (_isWindows()) {
print("\nSorry! MongoDB Shell Enhancements for Hackers isn't compatible with Windows.\n");
}
var ver = db.version().split(".");
if ( ver[0] <= parseInt("2") && ver[1] < parseInt("2") ) {
print(colorize("\nSorry! Mongo version 2.2.x and above is required! Please upgrade.\n", "red", true));
}
setVerboseShell(true);
setIndexParanoia(true);
setAutoMulti(true);
__indent = " "
function setIndexParanoia( value ) {
if( value == undefined ) value = true;
_indexParanoia = value;
}
function setAutoMulti( value ) {
if( value == undefined ) value = true;
_autoMulti = value;
}
function controlCode( parameters ) {
if ( parameters == undefined ) {
parameters = "";
}
else if (typeof(parameters) == 'object' && (parameters instanceof Array)) {
parameters = parameters.join(';');
}
return __ansi.csi + String(parameters) + String(__ansi.text_prop);
}
function applyColorCode( string, properties ) {
return controlCode(properties) + String(string) + controlCode();
}
function colorize( string, color, bright, underline ) {
var params = [];
var code = __ansi.foreground + __ansi.colors[color];
params.push(code);
if ( bright == true ) params.push(__ansi.bright);
if ( underline == true ) params.push(__ansi.underline);
return applyColorCode( string, params );
}
function getEnv(env_var){
clearRawMongoProgramOutput();
run('env');
var env = rawMongoProgramOutput();
var env_regex = new RegExp(env_var + '=(.*)');
return env.match(env_regex)[1];
}
ObjectId.prototype.toString = function() {
return this.str;
}
ObjectId.prototype.tojson = function(indent, nolint) {
return tojson(this);
}
DBCollection.prototype.filter = function( filter ) {
return new DBQuery( this._mongo, this._db, this, this._fullName, this._massageObject( filter ) );
}
DBQuery.prototype._validate = function( o ){
var firstKey = null;
for (var k in o) { firstKey = k; break; }
if (firstKey != null && firstKey[0] == '$') {
// for mods we only validate partially, for example keys may have dots
this._validateObject( o );
} else {
// we're basically inserting a brand new object, do full validation
this._validateForStorage( o );
}
}
DBQuery.prototype._validateObject = function( o ){
if (typeof(o) != "object")
throw "attempted to save a " + typeof(o) + " value. document expected.";
if ( o._ensureSpecial && o._checkModify )
throw "can't save a DBQuery object";
}
DBQuery.prototype._validateForStorage = function( o ){
this._validateObject( o );
for ( var k in o ){
if ( k.indexOf( "." ) >= 0 ) {
throw "can't have . in field names [" + k + "]" ;
}
if ( k.indexOf( "$" ) == 0 && ! DBCollection._allowedFields[k] ) {
throw "field names cannot start with $ [" + k + "]";
}
if ( o[k] !== null && typeof( o[k] ) === "object" ) {
this._validateForStorage( o[k] );
}
}
}
DB.prototype._getExtraInfo = function(action) {
if ( typeof _verboseShell === 'undefined' || !_verboseShell ) {
__callLastError = true;
return;
}
// explicit w:1 so that replset getLastErrorDefaults aren't used here which would be bad.
var res = this.getLastErrorCmd(1);
if (res) {
if (res.err != undefined && res.err != null) {
// error occurred, display it
print(res.err);
return;
}
var info = action + " ";
// hack for inserted because res.n is 0
info += action != "Inserted" ? res.n : 1;
if (res.n > 0 && res.updatedExisting != undefined) info += " " + (res.updatedExisting ? "existing" : "new")
info += " record(s) in ";
var time = new Date().getTime() - this.startTime;
var slowms = this.setProfilingLevel().slowms;
if (time > slowms) {
info += colorize(time + "ms", "red", true);
} else {
info += colorize(time + "ms", "green", true);
}
print(info);
}
}
DB.prototype.rename = function(newName) {
if(newName == this.getName() || newName.length == 0)
return;
this.copyDatabase(this.getName(), newName, "localhost");
this.dropDatabase();
db = this.getSiblingDB(newName);
}
DBQuery.prototype._checkMulti = function(){
if(this._limit > 0){
var ids = this.clone().select({_id: 1}).map(function(o){return o._id});
this._query['_id'] = {'$in': ids};
return true;
} else {
return false;
}
}
DBQuery.prototype.upsert = function( upsert ){
assert( upsert , "need an upsert object" );
this._validate(upsert);
this._db._initExtraInfo();
this._mongo.update( this._ns , this._query , upsert , true , false );
this._db._getExtraInfo("Upserted");
}
DBQuery.prototype.update = function( update ){
assert( update , "need an update object" );
this._checkMulti();
this._validate(update);
this._db._initExtraInfo();
this._mongo.update( this._ns , this._query , update , false , true );
this._db._getExtraInfo("Updated");
}
DBQuery.prototype.replace = function( replacement ){
assert( replacement , "need an update object" );
this._validate(replacement);
this._db._initExtraInfo();
this._mongo.update( this._ns , this._query , replacement , false , false );
this._db._getExtraInfo("Replaced");
}
DBQuery.prototype.remove = function(){
for ( var k in this._query ){
if ( k == "_id" && typeof( this._query[k] ) == "undefined" ){
throw "can't have _id set to undefined in a remove expression"
}
}
this._checkMulti();
this._db._initExtraInfo();
this._mongo.remove( this._ns , this._query , false );
this._db._getExtraInfo("Removed");
}
DBQuery.prototype.select = function( fields ){
this._fields = fields;
return this;
}
DBQuery.prototype.one = function(){
return this.limit(1)[0];
}
Date.prototype.tojson = function() {
var UTC = Date.printAsUTC ? 'UTC' : '';
var year = this['get'+UTC+'FullYear']().zeroPad(4);
var month = (this['get'+UTC+'Month']() + 1).zeroPad(2);
var date = this['get'+UTC+'Date']().zeroPad(2);
var hour = this['get'+UTC+'Hours']().zeroPad(2);
var minute = this['get'+UTC+'Minutes']().zeroPad(2);
var sec = this['get'+UTC+'Seconds']().zeroPad(2)
if (this['get'+UTC+'Milliseconds']())
sec += '.' + this['get'+UTC+'Milliseconds']().zeroPad(3)
var ofs = 'Z';
if (!Date.printAsUTC) {
var ofsmin = this.getTimezoneOffset();
if (ofsmin != 0){
ofs = ofsmin > 0 ? '-' : '+'; // This is correct
ofs += (ofsmin/60).zeroPad(2)
ofs += (ofsmin%60).zeroPad(2)
}
}
var date = colorize('"' + year + month + date + 'T' + hour +':' + minute + ':' + sec + ofs + '"', "cyan");
return 'ISODate(' + date + ')';
}
Array.tojson = function( a , indent , nolint ){
var lineEnding = nolint ? " " : "\n";
if (!indent)
indent = "";
if ( nolint )
indent = "";
if (a.length == 0) {
return "[ ]";
}
var s = "[" + lineEnding;
indent += __indent;
for ( var i=0; i<a.length; i++){
s += indent + tojson( a[i], indent , nolint );
if ( i < a.length - 1 ){
s += "," + lineEnding;
}
}
if ( a.length == 0 ) {
s += indent;
}
indent = indent.substring(__indent.length);
s += lineEnding+indent+"]";
return s;
}
NumberLong.prototype.tojson = function() {
return 'NumberLong(' + colorize(this.valueOf(), "red") + ')';
}
NumberInt.prototype.tojson = function() {
return 'NumberLong(' + colorize(this.valueOf(), "red") + ')';
}
tojson = function( x, indent , nolint ) {
if ( x === null )
return colorize("null", "red", true);
if ( x === undefined )
return colorize("undefined", "magenta", true);
if ( x.isObjectId ) {
return 'ObjectId(' + colorize('"' + x.str + '"', "green", false, true) + ')';
}
if (!indent)
indent = "";
switch ( typeof x ) {
case "string": {
var s = "\"";
for ( var i=0; i<x.length; i++ ){
switch (x[i]){
case '"': s += '\\"'; break;
case '\\': s += '\\\\'; break;
case '\b': s += '\\b'; break;
case '\f': s += '\\f'; break;
case '\n': s += '\\n'; break;
case '\r': s += '\\r'; break;
case '\t': s += '\\t'; break;
default: {
var code = x.charCodeAt(i);
if (code < 0x20){
s += (code < 0x10 ? '\\u000' : '\\u00') + code.toString(16);
} else {
s += x[i];
}
}
}
}
s += "\""
return colorize(s, "green", true);
}
case "number":
return colorize(x, "red")
case "boolean":
return colorize("" + x, "blue");
case "object": {
var s = tojsonObject( x, indent , nolint );
if ( ( nolint == null || nolint == true ) && s.length < 80 && ( indent == null || indent.length == 0 ) ){
s = s.replace( /[\s\r\n ]+/gm , " " );
}
return s;
}
case "function":
return colorize(x.toString(), "magenta")
default:
throw "tojson can't handle type " + ( typeof x );
}
}
tojsonObject = function( x, indent , nolint ) {
var lineEnding = nolint ? " " : "\n";
var tabSpace = nolint ? "" : __indent;
assert.eq( ( typeof x ) , "object" , "tojsonObject needs object, not [" + ( typeof x ) + "]" );
if (!indent)
indent = "";
if ( typeof( x.tojson ) == "function" && x.tojson != tojson ) {
return x.tojson(indent,nolint);
}
if ( x.constructor && typeof( x.constructor.tojson ) == "function" && x.constructor.tojson != tojson ) {
return x.constructor.tojson( x, indent , nolint );
}
if ( x.toString() == "[object MaxKey]" )
return "{ $maxKey : 1 }";
if ( x.toString() == "[object MinKey]" )
return "{ $minKey : 1 }";
var s = "{" + lineEnding;
// push one level of indent
indent += tabSpace;
var total = 0;
for ( var k in x ) total++;
if ( total == 0 ) {
s += indent + lineEnding;
}
var keys = x;
if ( typeof( x._simpleKeys ) == "function" )
keys = x._simpleKeys();
var num = 1;
for ( var k in keys ){
var val = x[k];
if ( val == DB.prototype || val == DBCollection.prototype )
continue;
s += indent + colorize("\"" + k + "\"", "yellow") + ": " + tojson( val, indent , nolint );
if (num != total) {
s += ",";
num++;
}
s += lineEnding;
}
// pop one level of indent
indent = indent.substring(__indent.length);
return s + indent + "}";
}
// Override group because map/reduce style is deprecated
DBCollection.prototype.agg_group = function( name, group_field, operation, op_value, filter ) {
var ops = [];
var group_op = { $group: { _id: '$' + group_field } };
if (filter != undefined) {
ops.push({ '$match': filter })
}
group_op['$group'][name] = { };
group_op['$group'][name]['$' + operation] = op_value
ops.push(group_op);
return this.aggregate(ops);
}
// Function that groups and counts by group after applying filter
DBCollection.prototype.gcount = function( group_field, filter ) {
return this.agg_group('count', group_field, 'sum', 1, filter);
}
// Function that groups and sums sum_field after applying filter
DBCollection.prototype.gsum = function( group_field, sum_field, filter ) {
return this.agg_group('sum', group_field, 'sum', '$' + sum_field, filter);
}
// Function that groups and averages avg_feld after applying filter
DBCollection.prototype.gavg = function( group_field, avg_field, filter ) {
return this.agg_group('avg', group_field, 'avg', '$' + avg_field, filter);
}
// Improve the default prompt with hostname, process type, and version
prompt = function() {
var serverstatus = db.serverStatus();
var host = serverstatus.host.split('.')[0];
var process = serverstatus.process;
var version = db.serverBuildInfo().version;
var repl_set = db.runCommand({"replSetGetStatus": 1})['ok'] != 0;
if(repl_set) {
var rs_state = db.isMaster().ismaster ? '[primary]' : '[secondary]';
} else {
var rs_state = '';
}
var mongos = db.isMaster().msg == 'isdbgrid';
var state = mongos ? '' : rs_state;
return host + '(' + process + '-' + version + ')' + state + ' ' + db + '> ';
}
DBQuery.prototype.shellPrint = function(){
try {
var start = new Date().getTime();
var n = 0;
while ( this.hasNext() && n < DBQuery.shellBatchSize ){
var s = this._prettyShell ? tojson( this.next() ) : tojson( this.next() , "" , true );
print( s );
n++;
}
var output = []
if (typeof _verboseShell !== 'undefined' && _verboseShell) {
var time = new Date().getTime() - start;
var slowms = this._db.setProfilingLevel().slowms;
var fetched = "Fetched " + n + " record(s) in ";
if (time > slowms) {
fetched += colorize(time + "ms", "red", true);
} else {
fetched += colorize(time + "ms", "green", true);
}
output.push(fetched);
}
if (typeof _indexParanoia !== 'undefined' && _indexParanoia) {
var explain = this.clone();
explain._ensureSpecial();
explain._query.$explain = true;
explain._limit = Math.abs(n._limit) * -1;
var result = explain.next();
var type = result.cursor;
var index_use = "Index["
if (type == "BasicCursor") {
index_use += colorize( "none", "red", true);
} else {
index_use += colorize( result.cursor.substring(12), "green", true );
}
index_use += "]";
output.push(index_use);
}
if ( this.hasNext() ) {
___it___ = this;
output.push("More[" + colorize("true", "green", true) + "]");
}
else {
___it___ = null;
output.push("More[" + colorize("false", "red", true) + "]");
}
print(output.join(" -- "));
}
catch ( e ){
print( e );
}
}