Aug/11
25
Using a unique index in MongoDB with Doctrine ODM and Symfony2
6 Comments · Posted by Christian in The real job
Just spent about an hour yesterday to figure out how to use unique indexes in a MongoDB document. It involves three simple steps.
I couldn’t get these right as they seem not to be documented in one place. With the help of two ServerGrove posts here and here I found out how to proceed.
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.
Doctrine2 · MongoDB · ODM · Symfony2
-
Henrik
-
Henrik
-
http://twitter.com/ludofleury Ludovic Fleury
-
caefer
<< Substract Google Plus – use Google Chrome incognito mode for vanilla search results

