【发布时间】: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