- $conn = new Mongo();
- //can be abbreviated as
- //$conn=new Mongo(); #Connect to local host, default port.
- //$conn= new Mongo(“172.21.15.69″); #Connect to the remote host
- //$conn=new Mongo(“xiaocai.loc:10086″); #Connect to the remote host on the specified port
- //$conn=new Mongo(“xiaocai. loc",array("replicaSet"=>true)); #Load balancing
- //$conn=new Mongo("xiaocai.loc",array("persist"=>"t")); #Persistent connection
- //$conn=new Mongo(“mongodb://sa:123@localhost”); #With username and password
-
- #Select test database
- $db=$conn->test;
- //$db= $conn->selectDB("test"); #Second way of writing
-
- #Select collection (select "table")
- $collection=$db->user;
- //$collection=$db-> selectCollection("user"); #The second way of writing
-
- #Insert operation
-
- $data=array("uid"=>"zz123","user_name"=>"Zhang San");
- $result= $collection->insert($data); #Simple insertion
- echo "Insert data id".$data["_id"];
-
- exit;
-
- #Insert operation safe insertion
- $data=array("uid "=>"zz124","user_name"=>"李思");
- $result=$collection->insert($data,true); #Used to wait for MongoDB to complete the operation to determine whether it was successful. (This parameter will be more useful when a large number of records are inserted)
-
- #Modification operation
- $where=array("uid"=>"zz123");
- $newdata=array("user_name"=>"张三三","tel"=>"123456789");
- $result=$collection->update($where,array('$set'=>$newdata));
-
- #Replace Update
- $ where=array("uid"=>"zz124");
- $newdata=array("user_age"=>"22","tel"=>"123456789");
- $result=$collection-> ;update($where,$newdata);
-
-
- #Batch update
- $where=array("uid"=>'zz');
- $newdata=array("user_name"=>"zz"," money"=>1000);
- $result=$collection->update($where,array('$set'=>$newdata),array('multiple'=>true));
-
- # Automatic accumulation
- $where=array('money'=>1000);
- $newdata=array('user_name'=>'edit');
- $result=$collection->update($where,array( '$set'=>$newdata,'$inc'=>array('money'=>-5)));
-
-
- #Delete node
- $where=array('uid'=>' zz124');
- $result=$collection->update($where,array('$unset'=>'tel'));//Delete node tel
-
- #Delete data
- $collection->remove (array('uid'=>'zz124'));
-
- #Delete the specified MongoId
- $id = new MongoId('4d638ea1d549a02801000011');
- $collection->remove(array('_id'=>( object)$id));
-
- #Query data Note: $gt means greater than, $gte means greater than or equal to, $lt means less than, $lte means less than or equal to, $ne means not equal to, $exists does not exist
- echo 'count :'.$collection->count()."
"; #All
- echo 'count:'.$collection->count(array('uid'=>'zz123'))."
"; #You can add conditions
- echo 'count:'.$collection->count(array('age'=>array('$gt'=>10,'$lte'=> ;30)))."
"; #greater than 50 and less than or equal to 74
- echo 'count:'.$collection->find()->limit(5)->skip(0)-> ;count(true)."
"; #Get the actual number of results returned
-
- #All documents in the collection
- $cursor = $collection->find()->snapshot();
- foreach ($ cursor as $id => $value) {
- echo "$id: "; var_dump($value);
- echo "
";
- }
-
- #Query a piece of data
- $cursor = $collection->findOne();
-
- #Exclude the column if false is not displayed
- $cursor = $collection->find()->fields(array("age" =>false,"tel"=>false));
- #Specify column true to display
- $cursor = $collection->find()->fields(array("user_name"=>true)) ;
-
- #(tel,age nodes exist) and age!=0 and age<50
- $where=array('tel'=>array('$exists'=>true),'age'=> array('$ne'=>0,'$lt'=>50,'$exists'=>true));
- $cursor = $collection->find($where);
-
- #Paging Get the result set
- $cursor = $collection->find()->limit(5)->skip(0);
-
- #Sort
- $cursor = $collection->find()->sort (array('age'=>-1,'type'=>1)); #1 means descending order -1 means ascending order, the order of parameters affects the sorting order
- #index
- $collection->ensureIndex(array( 'age' => 1,'money'=>-1)); #1 means descending order -1 means ascending order
- $collection->ensureIndex(array('age' => 1,'money'=> ;-1),array('background'=>true)); #Index creation is run in the background (the default is to run synchronously)
- $collection->ensureIndex(array('age' => 1,' money'=>-1),array('unique'=>true)); #This index is unique
-
-
-
- #Get query results
- $cursor = $collection->find();
- $ array=array();
- foreach ($cursor as $id => $value) {
- $array[]=$value;
- }
-
- #Close connection
- $conn->close();
- ?> ;
Copy code
|