Created
September 6, 2012 13:21
-
-
Save stamm/3656202 to your computer and use it in GitHub Desktop.
Mongo removing partials
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function microtime() { | |
return new Date().getTime() / 1000; | |
} | |
/** | |
* Удаляет данные из монги частями | |
* @param last_time - время последнего запроса | |
* @param collection - имя коллекции | |
* @param criteria - критерия для удаления | |
* @return {Array} | |
*/ | |
function delete_data(last_time, collection, criteria) { | |
var ids = [], | |
start_time = microtime(), | |
i = 0; | |
if (last_time < 10) { | |
count = 10000 + Math.floor((Math.random() * (20000-10000)) + 1); | |
} else { | |
count = 1000; | |
} | |
db[collection].find(criteria, {_id: 1}).limit(count).forEach(function(u){ | |
i++; | |
ids.push(u._id); | |
if (i % 3000 == 0) { | |
db[collection].remove({_id: {$in: ids }}); | |
ids = []; | |
} | |
}); | |
if (ids.length) { | |
db[collection].remove({_id: {$in: ids }}); | |
} | |
last_time = microtime() - start_time; | |
return [i, last_time]; | |
} | |
function delete(collection, criteria) { | |
var i = 0, | |
last_count = 1; | |
while (last_count && i < 1000) { | |
i++; | |
result = delete_data(last_time, collection, criteria); | |
last_count = result[0]; | |
last_time = result[1]; | |
print(last_count + ' ' + last_time); | |
if (i % 5 == 0) { | |
print("count: " + db[collection].count()); | |
} | |
} | |
} | |
delete('letter', {state: 10}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment