<pre><?PHP#phpinfo ();d ie;#other ways to link#$conn =new Mongo (); #连接本地主机, default port.#$conn =new Mongo ("127.0.0.1"); #连接远程主机#$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:[email protected]"); #带用户名密码//$conn =new Mongo ("mongodb://localhost:27017,localhost:27018"); #连接多个服务器//$conn =new Mongo ("Mongodb:///tmp/mongo-27017.sock"); #域套接字#$conn =new Mongo ("Mongodb://admin_miss:[email protected]:27017/test", Array (' persist ' = ' P ', ' replicaset ' = >true)); #完整 $conn=NewMongo ("127.0.0.1:27017");#connecting a specified port to a remote host$db=$conn->sky;#Select MyDB Database$collection=$db->bobo;#Select the collection (SELECT ' table ')//$collection = $db->selectcollection (myTable); #第二种写法//$collection = $conn->mydb->mytable; #更简洁的写法$array=Array( ' column_name ' = ' col '.Rand(100,999), ' column_exp ' = ' Xiaocai ' );$result=$collection->insert ($array);#Simple Insert//echo "new record ID:". $array [' _id ']; #MongoDB会返回一个记录标识#Var_dump ($result); #返回: bool (TRUE)//** inserts data into the collection safely, returning the Insert State (array). **/$array=Array( ' column_name ' = ' col '.Rand(100,999), ' column_exp ' = ' jshaibozhong ' );#$result = $collection->insert ($array, true); #用于等待MongoDB完成操作 to determine success. (This parameter is useful when you have a large number of records inserted)#echo "New record ID:". $array [' _id ']; #MongoDB会返回一个记录标识#Var_dump ($result); #返回: Array (3) {["Err"]=> NULL ["n"]=> int (0) ["OK"]=> float (1)}//** statistics Records **/Echo' Count: '.$collection-Count()." <br> ";#AllEcho' Count: '.$collection-Count(Array(' type ' = ' user '). " <br> ";#statistics ' type ' for user recordsEcho' Count: '.$collection-Count(Array(' Age ' =Array(' $gt ' =>50, ' $lte ' =>74)). " <br> ";#statistics greater than 50 less than or equal toEcho' Count: '.$collection->find ()->limit (5)->skip (0)Count(true)." <br> ";#get the number of results actually returned/** * Note: $GT is greater than, $GTE is greater than or equal to, $lt is less than, $lte is less than or equal, $ne is not equal to, $exists does not exist*/ //* * Get all records in the table **/$cursor=$collection->find ()snapshot ();foreach($cursor as $id=$value) {//echo "$id:"; Var_dump ($value); echo "<br>";}/** * NOTE: * After we have done the find () operation and obtained the $CURSOR cursor, the cursor is still dynamic. * In other words, after I find (), to my cursor loop to complete this time, if there is a qualifying record is inserted into the collection, Then these records will also be obtained by $cursor. * If you want the result set to not change after obtaining $cursor, you need to do this: * $cursor = $collection->find ();*///* * Query a data **/$cursor=$collection-FindOne ();/** * Note: FindOne () cannot use functions such as snapshot (), fields () after obtaining the result set;*///* * Set display field Age,type column does not display **/$cursor=$collection->find ()->fields (Array("Age" =false, "type" = =false));$cursor=$collection->find ()->fields (Array("User" =true));//Show only user columns/** * I'll make an error when I write this: $cursor->fields ("Age" =>true, "type" =>false);*///* * Set conditions (presence Type,age node) and age!=0 and age<50 **/$where=Array(' type ' = =Array(' $exists ' =true), ' age ' =Array(' $ne ' =>0, ' $lt ' =>50, ' $exists ' =true));$cursor=$collection->find ($where);//* * Paging get result set **/$cursor=$collection->find ()->limit (5)->skip (0);//* * Sort **/$cursor=$collection->find ()Sort(Array(' Age ' =>-1, ' type ' =>1));##1表示降序-1 means ascending, the order of the parameters affects the sort sequence//** index **/$collection->ensureindex (Array(' Age ' = 1, ' type ' =>-1));#1 means descending-1 means ascending$collection->ensureindex (Array(' Age ' = 1, ' type ' =>-1),Array(' background ' =true));#the creation of the index is run in the background (by default it runs synchronously)$collection->ensureindex (Array(' Age ' = 1, ' type ' =>-1),Array(' unique ' =true));#the index is unique/** * ENSUREINDEX (Array (), Array (' name ' = = ' index name ', ' Background ' =true, ' unique ' =true) * See: http://www.php.net/ manual/en/mongocollection.ensureindex.php*///* * Get query Results **/$cursor=$collection-find ();$array=Array();foreach($cursor as $id=$value) { $array[]=$value;} Print_r($array); //* * Delete node **///$where =array (' column_name ' = ' col685 ');//$result = $collection->update ($where, Array (' $unset ') = ' column_exp '));/** * Delete node Column_exp*//** * * Full format: Update (array $criteria, array $newobj [, array $options = Array ()]) * * NOTE: 1. Note to distinguish between replacement updates and modification updates * 2. Note the number of distinctions According to type such as array (' 91u ' = ' 684435 ') with array (' 91u ' =>684435) * **///* * Clear table **///$collection->remove (); #清空集合//** Delete the specified MongoId **///$id = new MongoId ("4d638ea1d549a02801000011");//$ Collection->remove (Array (' _id ' = = (object) $id)); * * Modify Update traditional update **/$where=Array("column_name" = "col123");$newdata=Array("Column_exp" = "Ggggg33sgg", "Column_fid" =>444);$result=$collection->update ($where,Array("\ $set" =$newdata));#$set: Make a node equal to a given value, similar to the $pull $pullAll $pop $inc, and slowly explain the usage later/** Result: * Original data * {"_id": ObjectId ("4d635ba2d549a02801000003"), "column_name": "col123", "Column_exp": "Xiaocai"}* was replaced by * {"_id": ObjectId ("4d635ba2d549a02801000003"), "column_name": "col123", "Column_exp": "Ggggggg", "Column_fid": 444}*///* * Replace update, overwrite original record **/$where=Array("column_name" = "col123");$newdata=Array("Column_exp" = "hhhhhhhhh", "Column_fid" =>123);$result=$collection->update ($where,$newdata);/** Result: * Original data * {"_id": ObjectId ("4d635ba2d549a02801000003"), "column_name": "col709", "Column_exp": "Xiaocai"}* was replaced by * {"_id": ObjectId ("4d635ba2d549a02801000003"), "Column_exp": "Hhhhhhhhh", "Column_fid": 123}*///* * Batch update **/$where=Array("column_name" = "col");$newdata=Array("column_exp" = "multiple", "91u" =>684435); $result=$collection->update ($where,Array("\ $set" =$newdata),Array("Multiple" =true));/** * all "column_name" = "col" are modified*///* * Auto-accumulate **/$where=Array("91u" =>684435);$newdata=Array("Column_exp" = "edit"); $result=$collection->update ($where,Array(' \ $set ' =$newdata, "\ $inc" =Array("91u" =>-5)));/** * Update 91u=684435 data and 91u auto-Subtract 5*/ $conn->close ();#Close Connection?>
"MongoDB Tutorial 13th Lesson" PHP MongoDB additions and deletions to use