Note that there are some explanatory texts on larger screens.

plurals
  1. PODoctrine Mongo ODM UniqueIndex is replicated
    primarykey
    data
    text
    <p>I'm playing with the UniqueIndex that I picked from the doc of Doctrine ODM and it seems I have a misanderstood of what it aims to do.</p> <p>Indeed I have a Keyword Document mapped by Doctrine ODM : </p> <pre><code>Namespace App\Document; use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; /** * @ODM\Document * @ODM\UniqueIndex(keys={"name"="asc", "lang"="asc"}) */ class Keyword { /** @ODM\Id(strategy="AUTO") */ protected $id; /** @ODM\String */ protected $name; /** @ODM\String */ protected $lang; .... </code></pre> <p>As you can see the Document has an uniqueIndex on 2 keys (Name and Lang)</p> <p>I have a all simple script which persist this Document</p> <pre><code> .... .... $keyword=new \App\Document\Keyword(); $keyword-&gt;setCreateDate(new \DateTime()); $keyword-&gt;setLang("fr"); $keyword-&gt;setLastParseDate(new \DateTime()); $keyword-&gt;setName("test"); $dm-&gt;persist($keyword); $dm-&gt;flush(); </code></pre> <p>Now when i find from mongo shell, my data with the same pair Name/lang are replicated when they should be unique :</p> <pre><code>&gt; db.Keyword.find() { "_id" : ObjectId("5171c72c6155795e47000000"), "name" : "test", "lang" : "fr", "createDate" : ISODate("2013-04-19T22:37:32Z"), "lastParseDate" : ISODate("2013-04-19T22:37:32Z") } { "_id" : ObjectId("5171c7366155796147000000"), "name" : "test", "lang" : "fr", "createDate" : ISODate("2013-04-19T22:37:42Z"), "lastParseDate" : ISODate("2013-04-19T22:37:42Z") } { "_id" : ObjectId("5171c7406155796447000000"), "name" : "test", "lang" : "fo", "createDate" : ISODate("2013-04-19T22:37:52Z"), "lastParseDate" : ISODate("2013-04-19T22:37:52Z") } { "_id" : ObjectId("5171c7fd615579a747000000"), "name" : "test", "lang" : "fo", "createDate" : ISODate("2013-04-19T22:41:01Z"), "lastParseDate" : ISODate("2013-04-19T22:41:01Z") } { "_id" : ObjectId("5171c7fe615579aa47000000"), "name" : "test", "lang" : "fo", "createDate" : ISODate("2013-04-19T22:41:02Z"), "lastParseDate" : ISODate("2013-04-19T22:41:02Z") } </code></pre> <p>My goal is to make the pair name/lang unique for persistence.</p> <p>So I finally have two questions :</p> <ul> <li>What UniqueIndex is made for ? (Because it doesnt prevent to replication)</li> <li>Should I use a <code>custom strategy</code> that concatenates Name and Lang as an unique id ? Is it a common usage ?</li> </ul> <p>EDIT :</p> <p>Thanks to @gview advices I found that i didnt ensureIndexes. I fixed thanks to this link : <a href="http://www.testically.org/2011/08/25/using-a-unique-index-in-mongodb-with-doctrine-odm-and-symfony2/" rel="nofollow">http://www.testically.org/2011/08/25/using-a-unique-index-in-mongodb-with-doctrine-odm-and-symfony2/</a></p> <p>But now instead of update my entry, it throws an error for duplicate entry. Should i use custom id as i said above ?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload