While loop to check database

Using the beforeInsert() data hook, you can check if the value to be inserted already exists in the database collection. If the value exists, the insert is rejected.

See the Example: No Database Duplicates .