-
- $conn = new Mongo();
- //可以簡寫
- //$conn=new Mongo (); #連接本地主機,預設連接埠.
- //$conn=new Mongo(“172.21.15.69″); #連接遠端主機
- //$conn=new Mongo(“xiaocai.loc:10086 ″); #連接指定連接埠遠端主機
- //$conn=new Mongo(“xiaocai.loc”,array(“replicaSet”=>true)); #負載平衡
- //$conn=new Mongo (“xiaocai.loc”,array(“persist”=>”t”)); #持久連線
- //$conn=new Mongo(“mongodb://sa:123@localhost”); #帶用戶姓名密碼
-
- #選擇test資料庫
- $db=$conn->test;
- //$db=$conn->selectDB("test"); #第二種寫法
-
- #選擇集合(選擇"表")
- $collection=$db->user;
- //$collection=$db->selectCollection("user"); #第二種寫法
-
- #插入操作
-
- $data=array("uid"=>"zz123","user_name"=>"張三");
- $result=$collection->insert ($data); #簡單插入
- echo "插入資料的id".$data["_id"];
-
- exit;
-
- #插入操作安全插入
- $ data=array("uid"=>"zz124","user_name"=>"李四");
- $result=$collection->insert($data,true); #用於等待MongoDB完成操作,以便確定是否成功.(當有大量記錄插入時使用該參數會比較有用)
-
- #修改操作
- $where=array("uid"=>"zz123");
- $ newdata=array("user_name"=>"張三三","tel"=>"123456789");
- $result=$collection->update($where,array('$set'=>$newdata ));
-
- #替換更新
- $where=array("uid"=>"zz124");
- $newdata=array("user_age"=>"22","tel" =>"123456789");
- $result=$collection->update($where,$newdata);
-
-
- #批次更新
- $where=array("uid"= >'zz');
- $newdata=array("user_name"=>"zz","money"=>1000);
- $result=$collection->update($where,array('$ set'=>$newdata),array('multiple'=>true));
-
- #自動累積
- $where=array('money'=>1000);
- $newdata= array('user_name'=>'edit');
- $result=$collection->update($where,array('$set'=>$newdata,'$inc'=>array('money'= >-5)));
-
-
- #刪除節點
- $where=array('uid'=>'zz124');
- $result=$collection->update($ where,array('$unset'=>'tel'));//刪除節點tel
-
- #刪除資料
- $collection->remove(array('uid'=>'zz124') );
-
- #刪除指定MongoId
- $id = new MongoId('4d638ea1d549a028010000011');
- $collection->remove(array('_id'=$idobject);
-
- #查詢資料註:$gt為大於、$gte為大於等於、$lt為小於、$lte為小於等於、$ne為不等於、$exists不存在
- echo 'count: '.$collection->count()."
"; #全部
- echo 'count:'.$collection->count(array('uid'=>'zz123'))."
"; #可以加上條件
- echo 'count:'.$collection->count(array('age'=>array('$gt'=>10,'$lte'=>30))). "
"; #大於50小於等於74
- echo 'count:'.$collection->find()->limit(5)->skip(0)->count(true)."
"; #取得實際回傳的結果數
-
- #集合中所有文件
- $cursor = $collection->find()->snapshot();
- foreach ($cursor as $id => $value) {
- echo "$id: "; var_dump($value);
- echo "
";
- }
-
- #查詢一條資料
- $cursor = $collection->findOne();
-
- #排除列false為不顯示
- $cursor = $collection->find() ->fields(array("age"=>false,"tel"=>false));
- #指定列true為顯示
- $cursor = $collection->find()->fields(array( "user_name"=>true));
-
- #(存在tel,age節點) and age!=0 and age$where=array('tel'=>array('$exists '=>true),'age'=>array('$ne'=>0,'$lt'=>50,'$exists'=>true));
- $cursor = $collection->find ($where);
-
- #分頁取得結果集
- $cursor = $collection->find()->limit(5)->skip(0);
-
- #排序
- $cursor = $collection->find()->sort(array('age'=>-1,'type'=>1)); #1表示降序-1表示升序,參數的先後影響排序順序
- #索引
- $collection->ensureIndex(array('age' => 1,'money'=>-1)); #1表示降序-1表示升序
- $collection->ensureIndex (array('age' => 1,'money'=>-1),array('background'=>true)); #索引的建立放在後台運行(預設是同步運行)
- $collection- >ensureIndex(array('age' => 1,'money'=>-1),array('unique'=>true)); #該索引是唯一的
-
-
-
- #取得查詢結果
- $cursor = $collection->find();
- $array=array();
- foreach ($cursor as $id => $value) {
- $array[ ]=$value;
- }
-
- #關閉連接
- $conn->close();
- ?>
複製代碼
|