-
Notifications
You must be signed in to change notification settings - Fork 235
/
Copy pathapi.js
203 lines (161 loc) · 6.32 KB
/
api.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
//----------------------------------------------------------------------------
// API Additions
//----------------------------------------------------------------------------
DBQuery.prototype.fields = function( fields ) {
this._fields = fields;
return this;
};
DBQuery.prototype.select = function( fields ){
this._fields = fields;
return this;
};
DBQuery.prototype.one = function(){
return this.limit(1)[0];
};
DBQuery.prototype.first = function(field){
var field = field || "$natural";
var sortBy = {};
sortBy[field] = 1;
return this.sort(sortBy).one();
}
DBQuery.prototype.reverse = function( field ){
var field = field || "$natural";
var sortBy = {};
sortBy[field] = -1;
return this.sort(sortBy);
}
DBQuery.prototype.last = function( field ){
var field = field || "$natural";
return this.reverse(field).one();
}
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);
};
DB.prototype.indexStats = function(collectionFilter, details){
details = details || false;
collectionNames = db.getCollectionNames().filter(function (collectionName) {
// exclude "system" collections from "count" operation
if (!collectionFilter) {
return !collectionName.startsWith('system.');
}
if (collectionName == collectionFilter) {
return !collectionName.startsWith('system.');
}
});
documentIndexes = collectionNames.map(function (collectionName) {
var count = db.getCollection(collectionName).count();
return (count.commify() + " document(s)");
});
columnSeparator = mongo_hacker_config['column_separator'];
assert(collectionNames.length == documentIndexes.length);
maxKeyLength = maxLength(collectionNames);
maxValueLength = maxLength(documentIndexes);
for (i = 0; i < collectionNames.length; i++) {
print(
colorize(collectionNames[i].pad(maxKeyLength, true), mongo_hacker_config['colors']['collectionNames'])
+ " " + columnSeparator + " "
+ documentIndexes[i].pad(maxValueLength)
);
var stats = db.getCollection(collectionNames[i]).stats();
var totalIndexSize = (Math.round((stats.totalIndexSize / 1024 / 1024) * 10) / 10) + " MB";
var indexNames = [];
var indexSizes = [];
for (indexName in stats.indexSizes) {
indexSizes.push((Math.round((stats.indexSizes[indexName] / 1024 / 1024) * 10) / 10) + " MB");
indexNames.push(" " + indexName);
}
maxIndexKeyLength = maxLength(indexNames);
maxIndexValueLength = maxLength(indexSizes);
print(
colorize("totalIndexSize".pad(maxKeyLength, true), mongo_hacker_config['colors']['string'])
+ " " + columnSeparator + " "
+ colorize(totalIndexSize.pad(maxValueLength), mongo_hacker_config['colors']['number'])
);
if (details) {
for (var j = 0; j < indexSizes.length; j++) {
print(
colorize("" + indexNames[j].pad(maxIndexKeyLength, true), mongo_hacker_config['colors']['string'])
+ " " + columnSeparator + " "
+ colorize(indexSizes[j].pad(maxIndexValueLength), mongo_hacker_config['colors']['binData'])
);
};
}
}
return "";
}
Mongo.prototype.getDatabaseNames = function() {
// this API addition gives us the following convenience function:
//
// db.getMongo().getDatabaseNames()
//
// which is similar in use to:
//
// db.getCollectionNames()
//
// mongo-hacker FTW :-)
return this.getDBs().databases.reduce(function(names, db) {
return names.concat(db.name);
}, []);
}
//----------------------------------------------------------------------------
// API Modifications (additions and changes)
//----------------------------------------------------------------------------
// Add upsert method which has upsert set as true and multi as false
DBQuery.prototype.upsert = function( upsert ){
assert( upsert , "need an upsert object" );
this._validate(upsert);
var startTime =
(typeof(_verboseShell) === 'undefined' || !_verboseShell) ? 0 : new Date().getTime();
this._mongo.update( this._ns , this._query , upsert , true , false );
this._db._printExtraInfo("Upserted", startTime);
};
// Updates are always multi and never an upsert
DBQuery.prototype.update = function( update ){
assert( update , "need an update object" );
this._checkMulti();
this._validate(update);
var startTime =
(typeof(_verboseShell) === 'undefined' || !_verboseShell) ? 0 : new Date().getTime();
this._mongo.update( this._ns , this._query , update , false , true );
this._db._printExtraInfo("Updated", startTime);
};
// Replace one document
DBQuery.prototype.replace = function( replacement ){
assert( replacement , "need an update object" );
this._validate(replacement);
var startTime =
(typeof(_verboseShell) === 'undefined' || !_verboseShell) ? 0 : new Date().getTime();
this._mongo.update( this._ns , this._query , replacement , false , false );
this._db._printExtraInfo("Replaced", startTime);
};
// Remove is always multi
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();
var startTime =
(typeof(_verboseShell) === 'undefined' || !_verboseShell) ? 0 : new Date().getTime();
this._mongo.remove( this._ns , this._query , false );
this._db._printExtraInfo("Removed", startTime);
};
//----------------------------------------------------------------------------
// Full Text Search
//----------------------------------------------------------------------------
DBQuery.prototype.textSearch = function( search ) {
var text = {
text: this._collection.getName(),
search: search,
filter: this._query,
project: this._fields,
limit: this._limit
}
var result = this._db.runCommand( text );
return result.results;
};