php操作mongodb

时间:2021-09-15 15:56:51
<?php
set_time_limit(0);
$mongo = new Mongo('192.168.33.50:27017'); //连接远程主机22011端口
$db = $mongo->test;
$collection = $db->user; //for ($i = 0;$i<100000;$i++) {
// $str = getRand();
// $arr = [
// 'id'=>$i,
// 'name'=>$str,
// 'age'=>rand(1,100)
// ];
// $collection->insert($arr);
//
//} $query = array('id'=>array('$lt'=>100));
$cursor = $collection->find($query); var_dump($cursor);
//遍历所有集合中的文档
foreach ($cursor as $obj)
{
echo $obj['_id']."<br>";
} function getRand()
{
$arr = range('a','z');
$str = join('',$arr);
$str = substr(str_shuffle($str),0,4);
return $str;
}
?>