PHP Mongo计数不适用于多个字段

时间:2013-08-11 16:02:33

标签: php mongodb

我有以下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一起使用。谁能告诉我哪里出错了?

1 个答案:

答案 0 :(得分: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);
    }
}