Skip to content

Indexing embedded document in different fields fails due duplicate name #1344

@Steveb-p

Description

@Steveb-p

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 :)

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions