My solution: https://gist.github.com/icaropires/96844821f7cca497a0ab908dfdbc0345
it uses batch operations, as previously suggested by @Gabriel Carballo