You are browsing a version that is no longer maintained.

Upserting Documents

Upserting documents in the MongoDB ODM is easy. All you really have to do is specify an ID ahead of time and Doctrine will perform an update operation with the upsert flag internally instead of a batchInsert.

Example:

1<?php $article = new Article(); $article->setId($articleId); $article->incrementNumViews(); $dm->persist($article); $dm->flush();
2
3
4
5
6
7

The above would result in an operation like the following:

1<?php $articleCollection->update( array('_id' => new MongoId($articleId)), array('$inc' => array('numViews' => 1)), array('upsert' => true, 'safe' => true) );
2
3
4
5
6
7

The extra benefit is the fact that you don't have to fetch the $article in order to append some new data to the document or change something. All you need is the identifier.