创建可以运行的集合和索引:
php app / console doctrine:mongodb:schema:create
架构更新
php app / console doctrine:mongodb:schema:update
注意:您可以看到其他命令: php app / console doctrine:mongodb
答案 0 :(得分:0)
Acme\DemoBundle\Document\Post:
repositoryClass: Acme\DemoBundle\Repository\PostMongoRepository
type: document
collection: post_test
fields:
id:
id: true
title:
type: string
index: asc
# php app/console doctrine:mongodb:generate:documents AcmeDemoBundle
# Create Document
# php app/console doctrine:mongodb:generate:repositories AcmeDemoBundle
# Create repository
现在运行php app / console doctrine:mongodbdb:schema:update
答案 1 :(得分:0)
// AppBundle/Document
namespace AppBundle\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Doctrine\Bundle\MongoDBBundle\Validator\Constraints\Unique as MongoDBUnique;
/**
* @MongoDB\Document(repositoryClass="AppBundle\Repository\ProduitRepository")
* @MongoDBUnique(fields="$noProduit")
*/
class Produit
{
/**
* @MongoDB\Id
*/
protected $id;
/**
* @MongoDB\String
* @MongoDB\UniqueIndex(order="asc")
*/
protected $noProduit;
/**
* @MongoDB\Field(type="string")
*/
protected $libelle;
// getter and setter
}
// AppBundle/Controller
$dm = $this->get('doctrine_mongodb')->getManager();
$dm->getSchemaManager()->ensureIndexes();
模式和数据库将在您运行应用程序时自动创建。 有关更多信息,visit this link