php操作mongodb的一些方法
阿新 • • 發佈:2018-12-24
$mongo_client = new MongoClient('mongodb://dev:[email protected]:27017');
$result = $mongo_client->connect();
echo 'result='.$result.'<br/>';
$records = $mongo_client->selectCollection('test','b_user');
echo 'collection b_user中有'.$records->count().'條資料<br/>';
//$records->remove(array('name'=>'wangfeifei'));
//$obj=array('name'=>'小云','age'=>21);
//$records->insert($obj);
//$obj=array('name'=>'胡呃呃','age'=>45,'position'=>'測試部經理');
//$records->insert($obj);
$collections = $records->find();
//var_dump($collections);
foreach($collections as $row)
{
echo $row['name']."<br/>";
}
echo '<hr/>';
$collections =$records->find(array('position'=>array('$not'=>new MongoRegex('/.*經理*./i'),'$exists'=>true),'age'=>array('$gt'=>40,'$exists'=>true)));
foreach($collections as $row)
{
echo $row['name']."混的太慘了<br/>";
}
echo '<hr/>';
$oneRecord = $records->findOne();
var_dump($oneRecord);
$name='胡';
$query=array("name"=>newMongoRegex("/.*”.$name.".*/i"));
$records->find($query);
$mongo_client->close();