mongodb-odm: Indexing embedded document in different fields fails due duplicate name
Hello,
I’ll go right to the point. When embedding a document class in two different fields and attempting to set an index on that document class, index cannot be created due to name conflict:
Trying to create an index with same name test with different key spec { property2.property_sub: 1 } vs existing spec { property1.property_sub: 1 }
Here are two classes that allow to reproduce the error.
Main class goes as follows:
<?php
namespace AppBundle\Document\Test;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
/**
* @ODM\Document
*/
class TestMain
{
/**
* @ODM\Id
*/
private $id;
/**
* @ODM\EmbedMany(targetDocument="TestSub")
*/
private $property1;
/**
* @ODM\EmbedMany(targetDocument="TestSub")
*/
private $property2;
}
Embedded class:
<?php
namespace AppBundle\Document\Test;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
/**
* @ODM\Document
* @ODM\Index(keys = {"property_sub" = "asc"}, name = "test")
*/
class TestSub
{
/**
* @ODM\Id
*/
private $id;
/**
* @ODM\Field(type="string")
*/
private $property_sub;
/**
* @ODM\Field(type="string")
*/
private $property_sub2;
}
I’ve tested this a little, and issue goes away if:
- name attribute for index is not used
- document is embedded just once
In my case, I’m bound to use name
because document is embedded deep in main document tree - otherwise I receive error regarding maximum index name length, which is already nicely mentioned in the docs.
As far as I’m concerned, I will probably create indexes manually, since I’m about to use a document in a “standalone” collection or embedded depending on specific application state, so specifying indexes in metadata won’t really work for me (I actually do not want those deep indexes, only on main collection to guarantee uniqueness of specific data sets).
However, I believe it is not an expected behaviour, so I’d like to share my case.
Thanks in advance, and thanks for a great library 😃
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 21 (16 by maintainers)
@Steveb-p took me a bit longer, but #1966 should fix your issue 👍
Just a very late follow-up to the problem @The-Don-Himself mentioned:
MongoDB 4.2 removes the 127 byte limit for index names once the
featureCompatibilityVersion
has been set to 4.2. That might help people that don’t want to manually set an index name.