The example code shows how to run cleanupOrphaned in a loop, so as to clean all orphans in a collection. This is more than just sample code, this is what is actually used in the field to clean up orphans on real systems.
Earlier on this page the conditions that can cause cleanupOrphaned to fail are explained.
However, this code does not check if the cleanupOrphaned command has failed (ie. it doesn't check the "ok" field of the command's result). This means that the script will plough on regardless, and doesn't inform the user of this problem. Suggest that it print out the whole result document, as well as an error if it detects result.ok != 1