1
我在工作中使用mongoDB作爲新項目,並且遇到了一些我找不到任何解決方案的麻煩。我在我的項目中有一個模型,它包含一個從MongoDB中獲取數據的方法,只要我使用findOne()而不是find()就可以工作。MongoDB在find()上返回空,但是在findOne上或者在使用count()時返回內容
的方法:
public function getData($input) {
$output = $this->_collection->find($input);
//$output = $this->_collection->findOne($input);
Zend_Debug::Dump($output);
return $output;
}
當運行一段代碼,我得到這個從轉儲:()
object(MongoCursor)#52 (0) {
}
使用findOne:
array(12) {
["_id"] => object(MongoId)#54 (1) {
["$id"] => string(24) "4fd85d178efd307080000001"
}
["autoadd"] => string(1) "1"
["name"] => string(5) "Sport"
["keyword"] => string(8) "cookie29"
["antal"] => string(1) "0"
["antal_week_date"] => string(10) "1218818007"
["version"] => string(1) "1"
["active"] => string(1) "1"
["antal_week"] => string(1) "0"
["customer_id"] => string(1) "2"
["id"] => string(2) "29"
["insert_date"] => string(10) "2007-11-21"
}
而且使用$output = $this->_collection->find($input)->count();
時:
int(20)
我在做什麼錯?這可能是一個簡單的問題,但我找不到任何其他方式來做這件事。如果你想知道輸入是什麼,它只是一個關聯數組:
$data = array('active' => '1');
請幫我取數據的所有20漂亮的「行」。 我將不勝感激。
感謝您的建議和更好的智慧!
好的,謝謝,我以爲我做了一些非常錯誤的事情。 只是感到驚訝,因爲findOne沒有返回遊標,我在讀的地方:http://www.php.net/manual/en/mongo.queries.php我沒有找到任何你的鏈接的例子。 謝謝。 – Ms01
這對PHP和其他一些驅動程序而言是不常見的功能。在其他語言中,即使在findOne()上也會返回遊標,但PHP驅動程序會爲您執行中間步驟,並從遊標獲取第一條記錄並將其作爲關聯數組返回。如果您在文檔中查找MongoCursor,您會看到它有從其獲取數據的方法和示例。 – Sammaye
@Sammaye:你可以在'findOne'返回光標時指定語言嗎?即使是mongo shell也會返回一個文檔,而不是光標。 –