Storage Strategies
Doctrine MongoDB ODM implements several different strategies for persisting changes to mapped fields. These strategies apply to the following mapping types:
int
float
- Embed Many
- Reference Many
For collections, Doctrine tracks changes via the PersistentCollection class. The
strategies described on this page are implemented by the CollectionPersister
class. The increment
strategy cannot be used for collections.
increment
The increment
strategy does not apply to collections but can be used for
int
, float
, decimal128
, and any custom type implementing the
\Doctrine\ODM\MongoDB\Types\Incrementable
interface. When using the
increment
strategy, the field value will be updated using the $inc
operator.
addToSet
The addToSet
strategy uses MongoDB's $addToSet operator to insert
elements into the array. This strategy is useful for ensuring that duplicate
values will not be inserted into the collection. Like the pushAll strategy,
elements are inserted in a separate query after removing deleted elements.
set
The set
strategy uses MongoDB's $set operator to update the entire
collection with a single update query.
Doctrine's Collection interface is modeled after PHP's associative arrays,
so they cannot always be represented as a BSON array. If the collection's
keys are not sequential integers starting with zero, the |
setArray
The setArray
strategy uses MongoDB's $set operator, just like the set
strategy, but will first numerically reindex the collection to ensure that it is
stored as a BSON array.
pushAll
The pushAll
strategy uses MongoDB's $push operator in combination with
$each to insert elements into the array. MongoDB does not allow elements to
be added and removed from an array in a single operation, so this strategy
relies on multiple update queries to remove and insert elements (in that order).
atomicSet
The atomicSet
strategy uses MongoDB's $set operator to update the entire
collection with a single update query. Unlike with set
strategy there will
be only one query for updating both parent document and collection itself. This
strategy can be especially useful when dealing with high concurrency and
versioned documents.
The |
atomicSetArray
The atomicSetArray
strategy works exactly like atomicSet
strategy, but
will first numerically reindex the collection to ensure that it is stored as a
BSON array.
The |