php mongoDB 的增/删/改(curd)操作

13年前
 $m = new Mongo("127.0.0.1");    //选择数据库  $db = $m -> wmt;    //选择集合  $collection = $db -> users;    $user1 = array('name' => 'hm', 'email' => 'admin@admin.com1','age'=>10);    $user2 = array('name' => 'hm', 'email' => 'admin@admin.com2','age'=>15);    $user3 = array('name' => 'hm', 'email' => 'admin@admin.com3','age'=>51);    $user4 = array('name' => 'hm', 'email' => 'admin@admin.com4','age'=>100);    $user5 = array('name' => 'hm', 'email' => 'admin@admin.com5','age'=>151);    $user6 = array('name' => 'hm', 'email' => 'admin@admin.com6','age'=>25);    $user7 = array('name' => 'hm', 'email' => 'admin@admin.com7','age'=>40);    $user8 = array('name' => 'mh', 'email' => 'admin@admin.com8','age'=>50);    $user9 = array('name' => 'mh', 'email' => 'admin@admin.com9','age'=>65);    $user10 = array('name' => 'mh', 'email' => 'admin@admin.com10','age'=>70);    $user11 = array('name' => 'mh', 'email' => 'admin@admin.com11','age'=>90);    $user12 = array('name' => 'mh', 'email' => 'admin@admin.com12','age'=>100);      //插入数据  /*$collection->insert($user1);  $collection->insert($user2);  $collection->insert($user3);  $collection->insert($user4);  $collection->insert($user5);  $collection->insert($user6);  $collection->insert($user7);  $collection->insert($user8);  $collection->insert($user9);  $collection->insert($user10);  $collection->insert($user11);  $collection->insert($user12);*/    //删除 name 为hm  //$collection -> remove(array('name'=>'hm'));    //删除所有的  //$collection -> remove();    //查询所有的记录  //$users = $collection->find();    //查询email为admin@admin.com12的记录的所有字段  //$users = $collection->find(array('email' =>'admin@admin.com12' ));    //查询email为admin@admin.com12 的name字段(注意默认会返回_id字段,其存储数据的唯一标识)  //$users = $collection->find(array('email' =>'admin@admin.com12' ),array('name'=>1));    //查询name 和email字段(注意默认会返回_id字段)  //$users = $collection -> find(array(),array('name'=>1,'email'=>1));    //查询name为hm的记录,并按照emai排序asc  //$users = $collection -> find(array('name'=>'hm'))->sort(array('email'=>1));      //查询name为hm的记录,并按照emai排序desc  //$users = $collection -> find(array('name'=>'hm'))->sort(array('email'=>-1));    //查询age大于50的记录  //$users = $collection -> find( array('age'=>array('$gt'=>50)));    //查询age小于50的记录  //$users = $collection -> find( array('age'=>array('$lt'=>50)));    //查询age大于50小于70的记录  //$users = $collection -> find( array('age'=>array('$lt'=>70,'$gt'=>50)));    //查询name like %h% 的记录  //$users = $collection -> find( array('name'=> new MongoRegex('/h/') ));    //查询name like h% 的记录  //$users = $collection -> find( array('name' => new MongoRegex('/^h/')));    //查询name like %h的记录  //$users = $collection -> find( array('name' => new MongoRegex('/h$/')));    //在name字段上创建索引  //$collection -> ensureIndex( array('name'=>1));    //创建聚簇索引  //$collection -> ensureIndex( array('name'=>1,'age'=>-1));    //查询name为hm age为10的记录  //$users = $collection -> find(array('name'=>'hm','age'=>10));    //查询name为hm 或者age为100的记录  //$users = $collection -> find( array('$or'=> array( array('name'=>'hm'),array('age'=>100 ) ) ));    //查询按照age升序排序后的第6到10条记录  //$users = $collection -> find() -> sort(array('age'=>1))->limit(5) -> skip(5);    //查询不重复的name  //$users = $db -> command( array('distinct'=>'users','key'=>'name'));    /*foreach($users as $user )  {      var_dump($user);                echo '<br/>';  }*/  //查询user的记录总数  /*$count = $collection -> count();  var_dump($count);die();*/    //查询age不为空的记录  /*$count = $collection -> find( array('age'=>array('$exists' =>true))) -> count();  var_dump($count);die();*/  //查询age大于30的记录总数  /*$count = $collection ->find(array('age'=>array('$gt'=>30)))->count();  var_dump($count);die();*/    //更新email为admin@admin.com1 的name为huangman  /*$collection -> update(array('email'=>'admin@admin.com1'),array('$set'=>array('name'=>'huangman')));  $users = $collection -> find(array('email'=>'admin@admin.com1'));*/    //更新email为admin@admin.com1 的记录age加2  /*$collection -> update(array('email' =>'admin@admin.com1'),array('$inc'=> array('age'=>2)));  $users = $collection -> find(array('email'=>'admin@admin.com1'));*/    /*foreach ($users as $user )  {      var_dump($user);  }*/