coen-hyde/Shanty-Mongo — Issue #65: Update using upsert
Author: shivagarwal
Created: 2012-01-27 01:12:51+00:00
State: open

Updating a document using upsert only creates a new record but when try to save the same document with an updated key value it creates a new record altogether instead of updating the existing document as it should with "upsert"

For example:

class User extends Shanty_Mongo_Document
{
protected static $_db = 'forum';
protected static $_collection = 'user';

protected static $_requirements = array(
'name' => 'Required',
'email' => array('Required', 'Validator:EmailAddress'),
'friends' => 'DocumentSet',
'friends.$' => array('Document:User', 'AsReference')
);
}

Now if you want to add or update a record based ont he criteria that email exists

$usr = new User();
//set up the values for user
$usr->update(array('email' => 'some@some.com', array('$set' => $usr->export()),array('upsert' => true));

the above should either create a new User document if the email does not exist or update and existing one combining the criteria and the update.

It successfully creates a new record on the first save but on the second save it creates a new record instead of updating the existing one.