尝试从DB获取唯一记录时未定义的方法MongoCollection :: command()

时间:2012-04-03 01:26:24

标签: php mongodb cursor distinct

当我尝试从我正在使用的MongoDB中获取唯一记录时,我收到以下错误 Fatal error: Call to undefined method MongoCollection::command() in /home/cr47/public_html/methods.php on line 67

我的代码正确列出了我的所有记录,但我不想列出重复项。所以我改变了我的列表记录功能(工作但是列出“Butter”20次而不是一次):

public function listRecords($query = null){
    $this->find($query);

    foreach($this->cursor as $record) {
        $ing = $record['Shrt_Desc'];

        echo '<a href="find.php?Shrt_Desc=' . $ing .'"> ' . $ing . ' </a> </br>';

    }
}

我修改了上面的功能(试图使“Butter”仅列出一次):

public function listRecords($query = null){
    $this->find($query);
    foreach($this->cursor as $record) {
        //$ing = $record['Shrt_Desc'];
        $ing = $this->command(array("distinct" => "record", "key" => "Shrt_Desc"));
        echo '<a href="find.php?Shrt_Desc=' . $ing .'"> ' . $ing . ' </a> </br>';
    }
}

这是distinct.php页面,我在其中实例化我的类并调用我的函数:

        include ("methods.php");
        $csvCommands = new csvCommands();
        $csvCommands->listRecords();

2 个答案:

答案 0 :(得分:1)

我认为问题在于你正在调用$this->find($query)你确定你有一个名为find的方法,或者你试图调用MongoDB查找方法它应该是$this->db->find$this->mongoDB->find

我认为最好上传您的完整电话,以便在您仍有问题时我们可以更好地查看

修改1

替换

$this->command

使用

$this->connection->command

由于

:)

答案 1 :(得分:0)

我刚尝试过MongoDB培训的一部分......它的工作原理......在我的思考之下      

$client = new MongoClient();

if($client){

/*echo "connection successufull";*/
$dbs=$client->school;
$studentcol=$dbs->students;
$lowest=[];
$scores=$studentcol->find();
foreach($scores as $score)
{
    $id=$score["_id"];
    $scoreslist=$score["scores"];
    var_dump($scoreslist)."</br>";
    $updated_sco=setnew_scores($scoreslist);
    echo "</br>";
    var_dump($updated_sco)."</br>";
    $update=update_score($studentcol,$id,$updated_sco);
    echo "old score list =>" .$scoreslist. " new score list is ".$updated_sco;

}
}

else
echo "connection un successfull";
?>
<?php
function setnew_scores($score)
{
    $counts=count($score);
    $counts;
    for ($i=0;$i<$counts;$i++)
    {
        if($score[$i]["type"] == "homework")
        {
            $lowest[$i]=$score[$i]["score"];    
        }

    }
    $lower = min($lowest);
    $j=0;
    for($i=0;$i<$counts;$i++)
    {   

        /*echo "</br> Type: ".$score[$i]["type"]."</br>";
        echo "</br> Scores: ".$score[$i]["score"]."</br>";
        echo "lower: ".$lower."</br>";*/
        if($score[$i]["score"] !=$lower ||  $score[$i]["type"]!=="homework")
        {
            $updated_scores[$j]["type"]=$score[$i]["type"];
            //echo $updated_scores[$j]["type"];
            $updated_scores[$j]["score"]=$score[$i]["score"];
            $j++;
        }
    }
    echo "</br>";   
    //print_r( $updated_scores)."</br>";
    return $updated_scores;
}
function update_score($scores,$id,$newscore)
{
    //print_r($newscore);/* its an array content*/
/*i tried the below type also its also works fine*/
    /*$newss="";
    for($i=0;$i<count($newscore);$i++)
    {
        $newss.="[\"".$newscore[$i]["type"]."\":".$newscore[$i]["score"]."]";
    }
    //echo $newss;
    //echo "</br>update([\"_id\":".$id."],['\$set'=>['scores' => ".$newss."]])";*/
    try{

        //$scores->update(["_id"=>$id],['$set'=>['scores' => $newss]]);
        $scores->update(["_id"=>$id],['$set'=>['scores' => $newscore]]);
    }
    catch(Exception $e){
        echo $e->get_Message();
    }
}