This repository was archived by the owner on Jul 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathlistKeysDetailed.js
More file actions
64 lines (54 loc) · 1.32 KB
/
listKeysDetailed.js
File metadata and controls
64 lines (54 loc) · 1.32 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
/**
* What are all the keys used in the collection "things" ? and how many times is each key used?
* Will recurse on all keys in a collection, is a lot slower than listKeys.js
*
* Adapated from the example in MongoDB the definitive guide
*/
collection = "things";
db.system.js.save({
_id: "emitr",
value: function(obj, prefix) {
var key;
for (key in obj) {
if (!obj.hasOwnProperty(key) || typeof obj[key] === 'function') {
continue;
}
if (typeof obj[key] === 'object') {
if (typeof key === 'number' || key.match(/^\d+$/)) {
emitr(obj[key], prefix);
continue;
} else {
emitr(obj[key], prefix + key + '.');
continue;
}
}
if (typeof key === 'number' || key.match(/^\d+$/) || key === 'floatApprox') {
emit(prefix.replace(/\.$/, ''), {count: 1});
} else {
emit(prefix + key, {count: 1});
}
}
}
});
mr = db.runCommand({
"mapreduce" : collection,
"map" : function() {
emitr(this, '');
},
"out": {replace: collection + "KeysDetailed"},
"reduce" : function(key, emits) {
var key, total = 0;
for (i in emits) {
total = total + emits[i].count;
}
return {count: total};
}
});
print();
print(collection + " keys:");
db[mr.result]
.find()
.forEach(function(row) {
prefix = "\t";
print(prefix + row._id + ': ' + row.value.count);
} );