Using a unique index in MongoDB with Doctrine ODM and Symfony2
Just spent about an hour yesterday to figure out how to use unique indexes in a MongoDB document. It involves three simple steps.
1. Add your unique constraint to your document
For other ways to specify unique indexes please refer to the official documentation.
2. Tell the document manager to create them
Apparently this does not happen automatically as you might expect.
3. Enforce the constraints in your code
It seems MongoDB would not fail on duplicate insert if you don’t tell it to.
Hope this helps someone.