-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmongo-explainer.js
More file actions
64 lines (59 loc) · 2.27 KB
/
mongo-explainer.js
File metadata and controls
64 lines (59 loc) · 2.27 KB
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
// Write your package code here!
// Variables exported by this module can be imported by other packages and
// applications. See mongo-explain-tests.js for an example of importing.
// import MongoInternals from 'meteor/mongo';
const Explainer = new Mongo.Collection('mongo-explainer');
const originalFind = MongoInternals.Connection.prototype.find;
MongoInternals.Connection.prototype.find = function(collectionName, selector, mod, options) {
try {
let collection = this.rawCollection(collectionName);
let query, explainSelector = "";
let cleanedSelector = looper(selector);
if (options) {
query = `${collectionName}.find(${JSON.stringify(cleanedSelector)}, ${JSON.stringify(options)})`;
explainSelector = `${cleanedSelector}, ${options}`;
} else {
query = `${collectionName}.find(${JSON.stringify(cleanedSelector)})`;
explainSelector = cleanedSelector;
}
let res = Explainer.upsert({query}, {$setOnInsert: {count: 1}, $inc: {count: 1}});
if (res.insertedId) {
let explainPromise = collection.find(explainSelector).explain();
explainPromise.then((explain) => {
let plan = explain.queryPlanner.winningPlan;
let filter = !!plan.filter;
let response = Explainer.upsert({_id: res.insertedId}, {$set: {stage: plan.stage, inputStage: plan.inputStage, filter}});
}).catch((error) => {console.log(error);});
}
} catch (e) {
console.log(e);
}
let returnVal = originalFind.apply(this, arguments);
return returnVal;
};
const looper = (selector) => {
let cleanedSelector = {};
_.each(selector, (val, key) => {
if (val === null) {
cleanedSelector[key] = null;
} else if (val.constructor === String || val.constructor === Number) {
let cleanVal = '123';
cleanedSelector[key] = cleanVal;
} else if (val.constructor === Array) {
if (val[0].constructor === String) {
let cleanVal = '123';
cleanedSelector[key] = [cleanVal];
} else {
let cleanVal = _.map(val, (v) => {
return looper(v);
});
cleanedSelector[key] = cleanVal;
}
} else if (val.constructor === Object) {
let cleanVal = looper(val);
cleanedSelector[key] = cleanVal;
}
});
return cleanedSelector
}
export const name = 'mongo-explain';