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
and float
fields. 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.
Note
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 set
strategy
will store the collection as a BSON object instead of an array. Use the
setArray strategy if you want to ensure that the collection is always
stored as a BSON array.
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 $pushAll operator 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.
Note
The atomicSet
and atomicSetArray
strategies may only be used for
collections mapped directly in a top-level document.
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.
Note
The atomicSet
and atomicSetArray
strategies may only be used for
collections mapped directly in a top-level document.