You are browsing a version that is no longer maintained.

Sharding

MongoDB allows you to horizontally scale your database. In order to enable this, Doctrine MongoDB ODM needs to know about your sharding setup. For basic information about sharding, please refer to the MongoDB docs.

Once you have a sharded cluster, you can enable sharding for a document. You can do this by defining a shard key in the document:

  • PHP
    1<?php /** * @Document * @ShardKey(keys={"username"="asc"}) */ class User { /** @Id */ public $id; /** @Field(type="int") */ public $accountId; /** @Field(type="string") */ public $username; }
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
  • XML
    1<doctrine-mongo-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mongo-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mongo-mapping http://doctrine-project.org/schemas/orm/doctrine-mongo-mapping.xsd"> <document name="Documents\User"> <shard-key> <key name="username" order="asc"/> </shard-key> </document> </doctrine-mongo-mapping>
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
  • YAML
    1Documents\User: shardKey: keys: username: asc
    2
    3
    4

When a shard key is defined for a document, Doctrine MongoDB ODM will no longer persist changes to the shard key as these fields become immutable in a sharded setup.

Once you've defined a shard key you need to enable sharding for the collection where the document will be stored. To do this, use the odm:schema:shard command.

For performance reasons, sharding is not enabled during the odm:schema:create and odm:schema:update commmands.