0

我有以下 PHP 函数,如果“revision_no”字段不匹配,它将 JSON 对象插入 mongodb,如果匹配,则更新这两个字段。

function saveJson($data){
    $mongo = new Mongo();
    $db = $mongo->selectDB("technical");
    $db->createCollection("ge",false);
    $technical = $db->selectCollection("ge");
    $json = json_decode($data['data']);
    $count = $technical->count(array('job_id' => $data['job_id'], 'revision_no' => $data['revision_no']));
    echo $count;
    if($count > 0){
        //With just job_id, it returns the correct count
        //$technical->update(array('job_id' => $data['job_id']), $json);
        $technical->update(array('job_id' => $data['job_id'], 'revision_no' => $data['revision_no']), $json);
    }else{
        $technical->insert($json);
    }
}

问题是我似乎无法使用 job_id 和 revision_no 的组合来计算计数;但它只适用于job_id。有人可以告诉我哪里出错了吗?

4

1 回答 1

0

原来我应该使用对象样式时使用的是数组语法!所以它不是 $data['revision_no'] 而是 $json->revision_no

function saveJson($data){
    $mongo = new Mongo();
    $db = $mongo->selectDB("technical");
    $db->createCollection("ge",false);
    $technical = $db->selectCollection("ge");
    $json = json_decode($data['data']);
    $count = $technical->count(array('job_id' => $data['job_id'], 'revision_no' => $json->revision_no));
    echo $count;
    if($count > 0){
        //With just job_id, it returns the correct count
        //$technical->update(array('job_id' => $data['job_id']), $json);
        $technical->update(array('job_id' => $data['job_id'], 'revision_no' => $json->revision_no), $json);
    }else{
        $technical->insert($json);
    }
}
于 2013-08-30T13:21:15.523 回答