-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
90 lines (71 loc) · 2.28 KB
/
index.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
var mongo = require('mongodb');
var async = require('async');
var {parse} = require('url');
module.exports = function(url, date, exclude, callback) {
var resultes = {};
var parsedUrl = parse(url)
var dbName = parsedUrl.pathname.slice(1)
mongo.MongoClient.connect(url, function (err, client) {
if (err) throw err;
var db = client.db(dbName);
db.collections(function (err, collections) {
var cols = [];
collections.forEach(function (col) {
var name = name = col.collectionName;
var isOps = name.indexOf('o_') === 0;
if (isOps && exclude.indexOf(name) === -1) {
cols.push(name);
}
});
if (!callback) console.log('Collections: ', cols.join(', '));
async.eachSeries(cols, cleanCollection.bind(null, db), function () {
client.close();
if (callback) {
callback(null, resultes);
} else {
process.exit();
}
});
});
});
function cleanCollection(db, oplogsCollectionName, done){
var snapshotsCollectionName = oplogsCollectionName.split('o_')[1];
if (!snapshotsCollectionName) {
throw new Error('Cant get collection name from ops-collection: ' + oplogsCollectionName);
}
var snapshotsCollection = db.collection(snapshotsCollectionName);
var oplogsCollection = db.collection(oplogsCollectionName);
var skip = 0;
var counter = 0;
async.forever(function(next) {
snapshotsCollection.find().project({
_id: 1,
_v: 1
}).limit(1000).skip(skip * 1000).toArray(function (err, snapshots) {
if (err) throw err;
snapshots = snapshots || [];
if (snapshots.length === 0){
return next('done');
}
async.eachSeries(snapshots, function (snapshot, cb) {
var query = {
'v': {$ne: snapshot._v - 1},
'm.ts': {$lt: date}
};
query.d = snapshot._id;
oplogsCollection.remove(query, function (err, res) {
counter += res.result.n;
cb()
});
}, function () {
if (!callback) console.log(oplogsCollectionName, counter);
skip++;
next();
});
});
}, function(){
resultes[oplogsCollectionName] = counter;
done();
});
}
};