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
|
//
// A view of a collection against which operations are explained rather than executed
// normally.
//
var Explainable = (function() {
var parseVerbosity = function(verbosity) {
// Truthy non-strings are interpreted as "allPlansExecution" verbosity.
if (verbosity && (typeof verbosity !== "string")) {
return "allPlansExecution";
}
// Falsy non-strings are interpreted as "queryPlanner" verbosity.
if (!verbosity && (typeof verbosity !== "string")) {
return "queryPlanner";
}
// If we're here, then the verbosity is a string. We reject invalid strings.
if (verbosity !== "queryPlanner" && verbosity !== "executionStats" &&
verbosity !== "allPlansExecution") {
throw Error("explain verbosity must be one of {" + "'queryPlanner'," +
"'executionStats'," + "'allPlansExecution'}");
}
return verbosity;
};
var throwOrReturn = function(explainResult) {
if (("ok" in explainResult && !explainResult.ok) || explainResult.$err) {
throw _getErrorWithCode(explainResult, "explain failed: " + tojson(explainResult));
}
return explainResult;
};
function constructor(collection, verbosity) {
//
// Private vars.
//
this._collection = collection;
this._verbosity = parseVerbosity(verbosity);
//
// Public methods.
//
this.getCollection = function() {
return this._collection;
};
this.getVerbosity = function() {
return this._verbosity;
};
this.setVerbosity = function(verbosity) {
this._verbosity = parseVerbosity(verbosity);
return this;
};
this.help = function() {
print("Explainable operations");
print("\t.aggregate(...) - explain an aggregation operation");
print("\t.count(...) - explain a count operation");
print("\t.distinct(...) - explain a distinct operation");
print("\t.find(...) - get an explainable query");
print("\t.findAndModify(...) - explain a findAndModify operation");
print("\t.group(...) - explain a group operation");
print("\t.remove(...) - explain a remove operation");
print("\t.update(...) - explain an update operation");
print("Explainable collection methods");
print("\t.getCollection()");
print("\t.getVerbosity()");
print("\t.setVerbosity(verbosity)");
return __magicNoPrint;
};
//
// Pretty representations.
//
this.toString = function() {
return "Explainable(" + this._collection.getFullName() + ")";
};
this.shellPrint = function() {
return this.toString();
};
//
// Explainable operations.
//
/**
* Adds "explain: true" to "extraOpts", and then passes through to the regular collection's
* aggregate helper.
*/
this.aggregate = function(pipeline, extraOpts) {
if (!(pipeline instanceof Array)) {
// support legacy varargs form. (Also handles db.foo.aggregate())
pipeline = Array.from(arguments);
extraOpts = {};
}
// Add the explain option.
extraOpts = extraOpts || {};
extraOpts.explain = true;
return this._collection.aggregate(pipeline, extraOpts);
};
this.count = function(query) {
return this.find(query).count();
};
/**
* .explain().find() and .find().explain() mean the same thing. In both cases, we use
* the DBExplainQuery abstraction in order to construct the proper explain command to send
* to the server.
*/
this.find = function() {
var cursor = this._collection.find.apply(this._collection, arguments);
return new DBExplainQuery(cursor, this._verbosity);
};
this.findAndModify = function(params) {
var famCmd = Object.extend({"findAndModify": this._collection.getName()}, params);
var explainCmd = {
"explain": famCmd,
"verbosity": this._verbosity
};
var explainResult = this._collection.runReadCommand(explainCmd);
return throwOrReturn(explainResult);
};
this.group = function(params) {
params.ns = this._collection.getName();
var grpCmd = {
"group": this._collection.getDB()._groupFixParms(params)
};
var explainCmd = {
"explain": grpCmd,
"verbosity": this._verbosity
};
var explainResult = this._collection.runReadCommand(explainCmd);
return throwOrReturn(explainResult);
};
this.distinct = function(keyString, query, options) {
var distinctCmd = {
distinct: this._collection.getName(),
key: keyString,
query: query || {}
};
if (options && options.hasOwnProperty("collation")) {
distinctCmd.collation = options.collation;
}
var explainCmd = {
explain: distinctCmd,
verbosity: this._verbosity
};
var explainResult = this._collection.runReadCommand(explainCmd);
return throwOrReturn(explainResult);
};
this.remove = function() {
var parsed = this._collection._parseRemove.apply(this._collection, arguments);
var query = parsed.query;
var justOne = parsed.justOne;
var collation = parsed.collation;
var bulk = this._collection.initializeOrderedBulkOp();
var removeOp = bulk.find(query);
if (collation) {
removeOp.collation(collation);
}
if (justOne) {
removeOp.removeOne();
} else {
removeOp.remove();
}
var explainCmd = bulk.convertToExplainCmd(this._verbosity);
var explainResult = this._collection.runCommand(explainCmd);
return throwOrReturn(explainResult);
};
this.update = function() {
var parsed = this._collection._parseUpdate.apply(this._collection, arguments);
var query = parsed.query;
var obj = parsed.obj;
var upsert = parsed.upsert;
var multi = parsed.multi;
var collation = parsed.collation;
var bulk = this._collection.initializeOrderedBulkOp();
var updateOp = bulk.find(query);
if (upsert) {
updateOp = updateOp.upsert();
}
if (collation) {
updateOp.collation(collation);
}
if (multi) {
updateOp.update(obj);
} else {
updateOp.updateOne(obj);
}
var explainCmd = bulk.convertToExplainCmd(this._verbosity);
var explainResult = this._collection.runCommand(explainCmd);
return throwOrReturn(explainResult);
};
}
//
// Public static methods.
//
constructor.parseVerbosity = parseVerbosity;
constructor.throwOrReturn = throwOrReturn;
return constructor;
})();
/**
* This is the user-facing method for creating an Explainable from a collection.
*/
DBCollection.prototype.explain = function(verbosity) {
return new Explainable(this, verbosity);
};
|