Mongodb-odm: Indexes missing / Single Collection Inheritance

Created on 17 Dec 2012  路  15Comments  路  Source: doctrine/mongodb-odm

Hi all,

I'm doing Single Collection Inheritance like this :

 @MongoDB\Document(
      collection="baseobjects",
      repositoryClass="PUC\CommonBundle\Repository\BaseObjectRepository",
      indexes={
         @MongoDB\Index(keys={"type"="asc"}),
      },
      requireIndexes=true
 )
 @MongoDB\InheritanceType("SINGLE_COLLECTION")
 @MongoDB\DiscriminatorField(fieldName="type")
 @MongoDB\DiscriminatorMap({
      "child" = "Child",
      ...
 })

class BaseObject
...


 @MongoDB\Document(
      collection="child",
      repositoryClass="PUC\CommonBundle\Repository\ChildRepository",
      indexes={
         @MongoDB\Index(keys={"email"="asc"})         
      },
      requireIndexes=true
 )

class Child extends BaseObject
...

Child indexes are not generated at BaseObject level (since Mongo 2.2.2) when using _doctrine:mongodb:schema:update/create_ command.

Since I require Indexes when querying Child objects (_requireIndexes=true_) it triggers an error : "Cannot execute unindexed queries on TEST\MyBundle\Document\Child. Unindexed fields: email"

I noticed that children collections are not physically created anymore in db since 2.2.2, which seems fair, but then the indexes shall be on mother class (which is stored), isn't it ? If I create one manually on mother class it works fine.

Thanks you for your feedback,

Cheers

Easy Pick Feature Hacktoberfest Stale

Most helpful comment

@getvivekv nobody stops you from doing so

All 15 comments

The problem seems to occur only if specifying Bundle/Document not when using the raw command.

+1 for this issue, I have the same too.

Damn! This issue was opened at Dec, 2012! Now it's the end of July, 2014. Why nobody fixed this for these two years?

Child indexes are not generated at BaseObject level (since Mongo 2.2.2) when using doctrine:mongodb:schema:update/create command.

It's not clear to me why the database version is significant here.

The problem seems to occur only if specifying Bundle/Document not when using the raw command.

Does that mean it's specific to DoctrineMongoDBBundle, and not ODM?

The thing that strikes me odd is that example's BaseObject is using collection baseobjects, declaring @MongoDB\InheritanceType("SINGLE_COLLECTION") and yet Child specifies collection child. @kevou84 (or @smilesrg as you were complaining as well yet haven't provided any details) would you mind shedding more light on the issue? Retriaging as a question until then.

@malarzm I faced with this issue a year ago, and I don;t remember now what exactly was wrong. As far as I remember, indexes from BaseObject are not generated but they should.

@smilesrg thanks, I'll try to write a failing test case or this

Ok, so I produced this test case:

<?php

namespace Doctrine\ODM\MongoDB\Tests\Functional\Ticket;

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

class GH465Test extends \Doctrine\ODM\MongoDB\Tests\BaseTest
{
    /**
     * @dataProvider provideClass
     */
    public function testCreatingIndexesForBaseDocument($class)
    {
        $indexes = $this->dm->getSchemaManager()->getDocumentIndexes($class);

        $this->assertCount(2, $indexes);

        $this->assertTrue(isset($indexes[0]['keys']['int']));
        $this->assertEquals(1, $indexes[0]['keys']['int']);

        $this->assertTrue(isset($indexes[1]['keys']['float']));
        $this->assertEquals(1, $indexes[1]['keys']['float']);
    }

    public function provideClass()
    {
        return array(
            array(__NAMESPACE__ . '\\GH465Base'),
            array(__NAMESPACE__ . '\\GH465Child'),
        );
    }
}

/**
 * @ODM\Document
 * @ODM\Index(keys={"int"="asc"})
 * @ODM\InheritanceType("SINGLE_COLLECTION")
 * @ODM\DiscriminatorField(fieldName="type")
 * @ODM\DiscriminatorMap({
 *  "child" = "GH465Child"
 * })
 */
class GH465Base
{
    /** @ODM\Id */
    public $id;

    /** @ODM\Int */
    public $int;
}

/**
 * @ODM\Document
 * @ODM\Index(keys={"float"="asc"})
 */
class GH465Child extends GH465Base
{
    /** @ODM\Float) */
    public $float;
}

and indeed indexes defined in GH465Child are not created when creating indexes for GH465Base. I also think that this is technically correct since we are generating indexes for a document and not for a whole collection.

On the other hand we could iterate through discriminatorMap and gather all indexes in SchemaManager but that would be a feature in my opinion :)

@malarzm does indexes that defined in GH465Base created when creating indexes for GH465Child?

@smilesrg yes they did, only test with GH465Base provided is failing

4 years and nobody fixed this

@getvivekv nobody stops you from doing so

and indeed indexes defined in GH465Child are not created when creating indexes for GH465Base. I also think that this is technically correct since we are generating indexes for a document and not for a whole collection.

The only thing worth checking out would be if inheritance_type is collection_per_class instead of single_collection, in which case creating indexes for GH465Child should also create indexes defined in GH465Base.

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in a week if no further activity occurs. Thank you for your contributions.

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in a week if no further activity occurs. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alcaeus picture alcaeus  路  10Comments

libressence picture libressence  路  11Comments

caiquecastro picture caiquecastro  路  9Comments

Steveb-p picture Steveb-p  路  10Comments

josefsabl picture josefsabl  路  6Comments