Instead of performing a check like this, have you considered putting a uniqueness constraint on the collection for task_id? That way you can ensure at the DB level that there are never two objects with the same task_id.