This repository was archived by the owner on Jun 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete_temporary_collections.php
More file actions
52 lines (42 loc) · 1.48 KB
/
delete_temporary_collections.php
File metadata and controls
52 lines (42 loc) · 1.48 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
<?php
/**
* Script to delete MongoDB temporary collections
* generally MongoDB tends to flush temporary collections but sometimes, it fails to do so leaving large number of unwanted collections
*/
function delete_temporary_collection($dbname) {
$connection = new Mongo();
$mongodb = $connection->$dbname;
$collections = $mongodb->listCollections();
$pattern1 = "/^tmp.mr.mapreduce_([0-9a-z_]*)$/";
$pattern2 = "/^mr.([a-z_]*).([0-9a-z_.]*)$/"; //used by MongoDB 1.2.x or earlier
/**
* this is for testing regex pattern
$arr = Array('mr.1260.551686_355_inc', 'tmp.mr.mapreduce_1260551686_355_inc', 'tmp.mr.mapreduce_1260551686_355', 'tmp.mr.mapreduce_1260551686_355_INC', 'mp.mr.mapreduce_1260551686_355_inc', 'tmp.mr.mapreduce1260551686_355_inc');
foreach ($arr as $a) {
$match1 = preg_match($pattern1, $a);
$match2 = preg_match($pattern2, $a);
if ($match1 === 1 || $match2 === 1) {
echo $a."\n";
}
else {
echo "Not matched for $a\n";
}
}
**/
$total_count = 0;
$removed_count = 0;
foreach ($collections as $collection) {
$name = $collection->getName();
$match = preg_match($pattern, $name);
if ($match === 1) {
$response = $collection->drop();
if ($response['ok'] == 1) {
// echo "removing $name... \n";
$removed_count++;
}
$total_count++;
}
}
echo "******************\nTOTAL TEMPORARY COLLECTIONS REMOVED = {$removed_count} out of {$total_count}\n*********************\n";
}
delete_temporary_collection('test_collections');