这是我php
从 mongoDB 插入和检索数据的脚本。在下面的插入工作和 find() 方法也工作。问题是在 find() 方法中使用条件时,它不会给出任何结果。这是为什么?
//object class
class data{
public $test1 = "default";
public $test2 = "default";
}
//creating 4 objects and change some values and insert to DB
$data1 = new data();
$data1->test1 = '2';
$data2 = new data();
$data2->test1 = '2';
$data3 = new data();
$data3->test1 = '3';
$data4 = new data();
$collection->insert([$data1, $data2, $data3, $data4]);
//viewing all (this is works - retrieve all inserted data)
$res = $collection->find();
foreach($res as $val){
var_dump($val);
}
//retrieve with condition (not works)
$q = array('test1' => '2');
$res2 = $collection->find($q);
foreach($res2 as $val2){
var_dump($val2);
}