MongoDB ODM索引:如何在具有嵌入式文档的文档中索引多个复合索引?

时间:2013-07-29 03:17:46

标签: mongodb doctrine-orm doctrine-odm odm

我有这个课程:

/**
 * @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;
}

我想在Product Document上应用多个复合索引。我想要添加到数据库的索引如下:

{ "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 }

我的索引注释是否正确? 似乎第一个索引必须正确,但我认为第二个和第三个索引是不正确的!

1 个答案:

答案 0 :(得分:2)

我认为ODM尚无法应用索引。您可能需要通过mongo.exe命令行中的命令应用索引:

use yourDbName
db.ensureIndexes()