PHP MongoDB更新不可变字段' _id'更新自己

时间:2014-10-30 08:59:34

标签: php mongodb

我正在尝试通过PHP更新MongoDB中的文档。

据我所知,语法似乎是正确的,但它会抛出此错误:

PHP Fatal error: Uncaught exception 'MongoWriteConcernException' with message 'localhost:27017: After applying the update to the document {_id: ObjectId('5451f5163690a91a198b4567') , ...}, the (immutable) field '_id' was found to have been altered to _id: ObjectId('5451fba53690a9ae1a8b4567')'

我的代码如下:

$val = array(
    'poiName' => 'Testname',
    'loc' => array((1.2345455, 9.98774949),
    'poiCategory' => 9,
    'cruiseName'=> 'testcruise',
    'poiId' => 123455677898,
    'source' => 'Rotiation',
    'sourceId' => 987654321,
    'user' => 'testuser',
    'createdAt' => new MongoDate($createdAtDateTimestamp),
    'createdAtRotate' => new MongoDate()
);

$pois[] = $val;

foreach($pois as $val){
    try{
        $col->save($val);
    } catch(Exception $ex){
        unset($val['createdAtRotate']);
        $newData = array('$set' => $val);
        $col->update(array('sourceId' => $val['sourceId']), $newData, array('upsert' => true));
    }
}

我正在catch块中进行更新,因为如果索引上已有文档匹配,它会在那里运行。通过更新,我不想更新'createdAtRoute'值。

我的错误在哪里?

0 个答案:

没有答案