索引未创建 [英] Index not created

查看:82
本文介绍了索引未创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个定义了索引的文档。

I have a document with an index defined.

/** @ODM\Document */
class Stat
{
    /** @ODM\Id(strategy="NONE", type="int") */
    private $id;

    /** @ODM\Field(type="string") */
    private $period;

    /** @ODM\ReferenceOne(targetDocument="Player") */
    private $player;

    /** @ODM\Field(type="string") @ODM\Index */
    private $statType;

但是,当我查看集合中创建的索引时,唯一的索引是_id。

However, when I look at the indexes created in the collection, the only index is the _id.

要保存我正在做的文档:

To save the document I'm doing:

$stat = new Documents\Stat();

$stat->setId(1);
$stat->setPlayer($player);
$stat->setPeriod(1);
$stat->setStatType('goal');

$dm->persist($stat);
$dm->flush();

还有其他我在做的事情吗?

Is there something else I'm not doing?

推荐答案

在正常ODM使用期间不会创建索引(也无法确保索引是否与映射同步)。

Indexes are not created (nor ensured if they're in sync with mapping) during normal ODM usage.

如果您使用的是Symfony,则解决问题很容易,只需从Symfony的控制台运行 doctrine:mongodb:schema:create 命令(如果使用的是其他框架,请查找

If you're using Symfony then solving problem is easy, just run doctrine:mongodb:schema:create command from Symfony's console (and if you're using other framework, look for equivalent as command itself is provided by ODM).

如果要通过代码控制索引,则可以使用 SchemaManager 其中提供直接控制模式的API(您也可以基于每个文档控制索引)。获得它很容易: $ sm = $ dm-> getSchemaManager();

If you want to control indexes from your code you can use SchemaManager which provides an API to control "schema" directly (you can also control indexes on per-document basis). Obtaining it is easy: $sm = $dm->getSchemaManager();.

这篇关于索引未创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆