MongoDB ODM Indexing : How to Index multiple Compound index on a documnt that have EmbeddedDocument in itself?

StackOverflow https://stackoverflow.com/questions/17915543

Question

I have this classes:

/**
 * @ODM\Document
 * @Indexes({
 *   @Index(keys={"status"="asc", "regDate"="desc", "expDate"="asc", "isFeatured"="asc"}),
 *   @Index(keys={"status"="asc", "visits.total"="asc", "visists.today"="asc"}),
 *   @Index(keys={"status"="asc", "price.value"="asc", "regDate"="asc"})
 * })
 */
class Product {

    /**
     * @ODM\Date
     */
    protected $regDate;

    /**
     * @ODM\Date
     */
    protected $expire;

    /**
     * @ODM\EmbedOne(targetDocument="Price")
     */
    protected $price;

    /**
     * @ODM\Boolean
     */
    protected $isFeatured;

    /**
     * @ODM\EmbedMany(targetDocument="Visit")
     */
    protected $visits;
}

/**
 * @ODM\EmbeddedDocument
 */
class Price {
    /**
     * @ODM\Int
     */
    protected $value;

    /**
     * @ODM\String
     */
    protected $currency;
}

/**
 * @ODM\EmbeddedDocument
 */
class Visit {
    /**
     * @ODM\Int
     */
    protected $total;

    /**
     * @ODM\Int
     */
    protected $today;

    /**
     * @ODM\EmbedMany(targetDocument="VisitPerDate")
     */
    protected $perDate = array();
}

/**
 * @ODM|EmbeddedDocument
 */
class VisitPerDate {
    /**
     * @ODM\Date
     */
    protected $date;

    /**
     * @ODM\Int
     */
    protected $visit;
}

I want to apply multiple compound indexes on Product Document.The indexes i wanna to add to database are the following:

{ "status"=1, "regDate"=-1, "expDate"=1, "isFeatured"=1 }
{ "status"=1, "visits.total"=1, "visits.today"=1, "regDate"=1 }
{ "status"=1, "price.value"=1, "regDate"=1 }

Was my Indexing annotation correct? It seems that the first index must be correct,but i think the second and the third index is incorrect!

Was it helpful?

Solution

I think applying indexes is not possible yet by ODM. You may need to apply indexes by a command like this in mongo.exe command line:

use yourDbName
db.ensureIndexes()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top