【问题标题】:Undefined method MongoCollection::command() when trying to get unique records from DB尝试从数据库获取唯一记录时未定义的方法 MongoCollection::command()
【发布时间】:2012-04-03 01:26:24
【问题描述】:

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

我的代码正确列出了我的所有记录,但我不想列出重复项。所以我改变了我的列表记录功能(有效但列出“黄油”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();

【问题讨论】:

  • 我还应该补充一点,我有一个处理 mongo 连接、数据库命名和定义查找查询的数据库类: protected function find($query = null){ if ($query == null) { $this->cursor = $this->collection->find(); } else{ $this->cursor = $this->collection->find($query); }

标签: php mongodb cursor distinct


【解决方案1】:

我认为问题在于您正在调用$this-&gt;find($query),您确定您有一个名为find 的方法,或者您正在尝试调用MongoDB find 方法,它应该类似于$this-&gt;db-&gt;find$this-&gt;mongoDB-&gt;find

我认为最好上传您的完整通话,以便如果您仍有问题,我们可以更好地查看它

编辑 1

替换

$this->command

$this->connection->command

谢谢

:)

【讨论】:

  • 是的,我定义了 find 方法,并用它对问题发表了评论。
  • 查找部分工作正常。这是不同的部分。
  • 请参阅php.net/manual/en/mongodb.command.php ..... 并查看我编辑的答案
  • 我试过 $this->connection->command $this->connection->collection->command $this->collection->command 似乎没有任何效果
【解决方案2】:

我刚刚尝试作为 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();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-26
    • 1970-01-01
    • 1970-01-01
    • 2019-09-06
    相关资源
    最近更新 更多