Basically try to do the same as:
UPDATE table SET column3 = column1 + column2;
In the Doctrine2’s ODM QueryBuilder – is something like this possible with out MapReduce? I have a sharded collection, so I need to use the QueryBuilder in order to search by my compound shard key.
Simplified exmaple:
$visit = $this->dm->createQueryBuilder('Visit')
->findAndUpdate()
->field('site')->equals( $site )
->field('timeStamp')->equals( $ts )
->field('_id')->equals( $id )
->update()
->field('column3')->set( 'column1 + column2' );
Unfortunately, that would set column3 to the string “column1 + column2”. Any ideas?
Unfortunately, neither
$incnor$setwill help here, as they expect literal values. I also don’t think findAndModify would help, since it uses the same modifier object asupdate().I don’t think map/reduce is appropriate either, as that isn’t intended to modify the documents being mapped.
If you weren’t in a sharded infrastructure, you could use db.eval() to fetch and update the object atomically; however, that isn’t an option.
I would suggest splitting this into two queries. First, find the object by its
site,timestampand_id. You can limit the fields returned tocolumn1andcolumn2. Then, issue an update with the same criteria plus a match oncolumn1andcolumn2, and setcolumn3to their sum. The addition ofcolumn1andcolumn2to the update criteria will ensure you don’t inadvertently store an invalid if those fields were changed between the find and update queries. And effectively, your update query becomes idempotent.