-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.mongorc.js
86 lines (68 loc) · 2.61 KB
/
.mongorc.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
db.getMongo().setReadPref('secondary')
var oldFind = DBCollection.prototype.find;
DBCollection.prototype.find = function(query, fields, limit, skip, batchSize, options) {
var isOrganization = this.toString().match(/organizations/);
var isCall = this.toString().match(/calls/);
var isByID = query instanceof ObjectId;
if ((!query._organization && !query._id) && !isOrganization && isCall && !isByID) {
throw "Add _organization or _id to your query";
}
if (!query.createdDate && isCall && !isByID) {
const date = new ISODate();
date.setHours(0);
throw `Add a createdDate to this query :) what about : { createdDate: { \$gt: ISODate('${date.toISOString()}') } }`;
}
return oldFind.call(this, query, fields, limit, skip, batchSize, options);
};
var oldCount = DBCollection.prototype.count;
DBCollection.prototype.count = function(query, fields, limit, skip, batchSize, options) {
var isOrganization = this.toString().match(/organizations/);
var isCall = this.toString().match(/calls/);
if (((!query._organization && !query._id) && !isOrganization) && isCall) {
throw "Add _organization or _id to your query";
}
if (!query.createdDate && this.toString().match(/calls/)) {
throw "Add a createdDate to this query :)";
}
return oldCount.call(this, query, fields, limit, skip, batchSize, options);
};
// Export to CSV function
DBQuery.prototype.toCSV = function(deliminator, textQualifier) {
var count = -1;
var headers = [];
var data = {};
var cursor = this;
deliminator = deliminator == null ? ',' : deliminator;
textQualifier = textQualifier == null ? '\"' : textQualifier;
while (cursor.hasNext()) {
var array = new Array(cursor.next());
count++;
for (var index in array[0]) {
if (headers.indexOf(index) == -1) {
headers.push(index);
}
}
for (var i = 0; i < array.length; i++) {
for (var index in array[i]) {
data[count + '_' + index] = array[i][index];
}
}
}
var line = '';
for (var index in headers) {
line += headers[index] + ',';
}
line = line.slice(0, -1);
print(line);
for (var i = 0; i < count + 1; i++) {
var line = '';
var cell = '';
for (var j = 0; j < headers.length; j++) {
cell = data[i + '_' + headers[j]];
if (cell == undefined) cell = '';
line += textQualifier + cell + textQualifier + deliminator;
}
line = line.slice(0, -1);
print(line);
}
}