0

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?

kmfk
  • 3,821
  • 2
  • 22
  • 32

1 Answers1

1

Unfortunately, neither $inc nor $set will help here, as they expect literal values. I also don't think findAndModify would help, since it uses the same modifier object as update().

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, timestamp and _id. You can limit the fields returned to column1 and column2. Then, issue an update with the same criteria plus a match on column1 and column2, and set column3 to their sum. The addition of column1 and column2 to 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.

jmikola
  • 6,892
  • 1
  • 31
  • 61
  • Thanks for the response! Yeah, I was hoping to avoid a second query - but your approach makes sense. – kmfk Jun 26 '12 at 20:25