In depth technical summary of PHP operation MongoDB

  • 2020-06-03 06:07:45
  • OfStack


<?php
/**
* PHP operation MongoDB Learning notes 
*/
//*************************
//**    The connection MongoDB The database   **//
//*************************
// format =>( " mongodb:// The user name : password  @ address : port / Default specified database" , parameter )
$conn = new Mongo();
// We could write it as 
//$conn=new Mongo(); # Connect to localhost , The default port .
//$conn=new Mongo( " 172.21.15.69 " ); # Connect to remote host 
//$conn=new Mongo( " xiaocai.loc:10086 " ); # Connect to a remote host on the specified port 
//$conn=new Mongo( " xiaocai.loc " ,array( " replicaSet " =>true)); # Load balancing 
//$conn=new Mongo( " xiaocai.loc " ,array( " persist " => " t " )); # A persistent connection 
//$conn=new Mongo( " mongodb://sa:123@localhost " ); # Bring the username and password 
//$conn=new Mongo( " mongodb://localhost:27017,localhost:27018 " ); # Connect to multiple servers 
//$conn=new Mongo( " mongodb:///tmp/mongo-27017.sock " ); # Domain socket 
//$conn=new Mongo( " mongodb://admin_miss:miss@localhost:27017/test " ,array( ' persist'=>'p', " replicaSet " =>true)); # complete 
// Detailed information on :http://www.php.net/manual/en/mongo.connecting.php
//*************************
//**    Select database and table     **//
//*************************
$db=$conn->mydb; # choose mydb The database 
//$db=$conn->selectDB( " mydb " ); # The first 2 Kind of writing 
$collection=$db->column; # Choice set ( choose ' table ')
//$collection=$db->selectCollection( ' column'); # The first 2 Kind of writing 
//$collection=$conn->mydb->column; # Let me write it a little bit more succinctly 
// Pay attention to :1. Databases and collections do not need to be created beforehand , They are automatically created if they do not exist .
//   2. Watch out for typos , You might inadvertently create one 1 A new database ( With the original database chaos ).
//*************************
//**    Inserted into the document      **//
//*************************
//** Inserts data into the collection , return bool Determines whether the insert was successful . **/
$array=array( ' column_name'=>'col'.rand(100,999),'column_exp'=>'xiaocai');
$result=$collection->insert($array); # A simple insert 
echo  "The new record ID: " .$array['_id']; #MongoDB Returns the 1 Each record identification 
var_dump($result); # return :bool(true)
//** Securely insert data into the collection , Return to insert status ( An array of ). **/
$array=array( ' column_name'=>'col'.rand(100,999),'column_exp'=>'xiaocai2 ' );
$result=$collection->insert($array,true); # For waiting for MongoDB To complete the operation , To determine if it was successful .( It is useful to use this parameter when a large number of records are inserted )
echo  "The new record ID: " .$array['_id']; #MongoDB Returns the 1 Each record identification 
var_dump($result); # return :array(3) { ["err"]=> NULL ["n"]=> int(0) ["ok"]=> float(1) }
//** Let me write it in full  **/
#insert($array,array( ' safe'=>false,'fsync'=>false,'timeout'=>10000))
/*
* *
*  Full format :insert ( array $a [, array $options = array() ] )
*    insert(array(),array( ' safe'=>false,'fsync'=>false,'timeout'=>10000))
*        parameter :safe: The default false, Whether it is safe to write 
*   fsync: The default false, Whether to force insert into sync to disk 
*     timeout: timeout ( ms )
*
*  Insert the results :{  " _id "  : ObjectId( " 4d63552ad549a02c01000009 " ),  " column_name "  :  " col770 " ,  " column_exp "  :  " xiaocai "  }
*    '_id' Primary key field , The insert is MongoDB Automatically add .
*
*     Pay attention to :1. The following two inserts are the same 1 records ( The same _id), Because they have the same value. 
*         $collection->insert(array( ' column_name'=>'xiaocai'));
*         $collection->insert(array( ' column_name'=>'xiaocai'));
*      avoid 
* $collection->insert(array( ' column_name'=>'xiaocai'),true);
* try {
*      $collection->insert(array( ' column_name'=>'xiaocai'),true);
* }catch(MongoCursorException $e){
*      echo  " Can't save the same person twice!\n " ;
* }
*
*     Detailed information on :http://www.php.net/manual/zh/mongocollection.insert.php
* *
*/
//*************************
//**    Update the document      **//
//*************************
//**  Modify the update  **/
$where=array( ' column_name'=>'col123 ' );
$newdata=array( ' column_exp'=>'GGGGGGG','column_fid'=>444);
$result=$collection->update($where,array( ' $set'=>$newdata)); #$set: Let a node be equal to a given value , Similar to that $pull $pullAll $pop $inc, Use is explained slowly later 
/*
*  The results of :
*  The original data 
* { " _id " :ObjectId( " 4d635ba2d549a02801000003 " ), " column_name " : " col123 " , " column_exp " : " xiaocai " }
*  Replaced by 
* { " _id " :ObjectId( " 4d635ba2d549a02801000003 " ), " column_name " : " col123 " , " column_exp " : " GGGGGGG " , " column_fid " :444}
*/
//**  Replace the update  **/
$where=array( ' column_name'=>'col709 ' );
$newdata=array( ' column_exp'=>'HHHHHHHHH','column_fid'=>123);
$result=$collection->update($where,$newdata);
/*
*  The results of :
*  The original data 
* { " _id " :ObjectId( " 4d635ba2d549a02801000003 " ), " column_name " : " col709 " , " column_exp " : " xiaocai " }
*  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 
*/
//**  Automatic accumulation  **/
$where=array('91u'=>684435);
$newdata=array( ' column_exp'=>'edit');
$result=$collection->update($where,array( ' $set'=>$newdata,'$inc'=>array('91u'=>-5)));
/**
*  update 91u=684435 The data of , and 91u Since the reduction of 5
*/
/**  Remove nodes  **/
$where=array( ' column_name'=>'col685 ' );
$result=$collection->update($where,array( ' $unset'=>'column_exp'));
/**
*  Remove nodes column_exp
*/
/*
* *
*  Full format :update(array $criteria, array $newobj [, array $options = array()  ] )
*        Pay attention to :1. Note the distinction between replacement updates and modification updates 
*    2. Note the distinction between data types such as  array('91u'=>'684435 ' ) with array('91u'=>684435)
*  Detailed information on :http://www.mongodb.org/display/DOCS/Updating#Updating-%24bit
* *
*/
//*************************
//**    Delete the document      **//
//*************************
/**  Clear the database  **/
$collection->remove(array( ' column_name'=>'col399 ' ));
//$collection->remove(); # Empty the collection 
/**  Delete the specified MongoId **/
$id = new MongoId( " 4d638ea1d549a02801000011 " );
$collection->remove(array( ' _id'=>(object)$id));
/*
* *
*   Use the following method to match { " _id " :ObjectId( " 4d638ea1d549a02801000011 " )}, Query, update also 1 sample 
*  $id = new MongoId( " 4d638ea1d549a02801000011 " );
*  array( ' _id'=>(object)$id)
* *
*/
//*************************
//**    Query the document      **//
//*************************
/**  Query the number of records in the document  **/
echo  ' count:'.$collection->count(). " <br> " ; # all 
echo  ' count:'.$collection->count(array( ' type'=>'user')). " <br> " ; # You can add conditions 
echo  ' count:'.$collection->count(array( ' age'=>array( ' $gt'=>50,'$lte'=>74))). " <br> " ; # Is greater than 50 Less than or equal to 74
echo  ' count:'.$collection->find()->limit(5)->skip(0)->count(true). " <br> " ; # Gets the actual number of results returned 
/**
*  note :$gt For more than, $gte Is greater than or equal to, $lt For less than, $lte Is less than or equal to, $ne Is not equal to, $exists There is no 
*/
/**  All documents in the collection  **/
$cursor = $collection->find()->snapshot();
foreach ($cursor as $id => $value) {
echo  " $id:  " ; var_dump($value); echo  " <br> " ;
}
/**
*  Pay attention to :
*  Before we did find() Operate, obtain $cursor After the cursor, the cursor is still dynamic .
*  In other words, , In my find() after , The loop to my cursor completes this time , If any more qualifying records are inserted into the collection, Then these records will also be $cursor  To obtain .
*  If you want to get in $cursor The subsequent result set does not change , Here's what you need to do: 
* $cursor = $collection->find();
* $cursor->snapshot();
*  As shown in the http://www.bumao.com/index.php/2010/08/mongo_php_cursor.html
*/
/**  The query 1 The data  **/
$cursor = $collection->findOne();
/**
*   Pay attention to :findOne() Cannot be used after obtaining the result set snapshot(),fields() Such as function ;
*/
/** age,type  Columns do not display  **/
$cursor = $collection->find()->fields(array( " age " =>false, " type " =>false));
/**  Display only user  column  **/
$cursor = $collection->find()->fields(array( " user " =>true));
/**
*  I'm going to make a mistake writing it this way :$cursor->fields(array( " age " =>true, " type " =>false));
*/
/** ( There are 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 gets the result set   **/
$cursor = $collection->find()->limit(5)->skip(0);
/**  The sorting   **/
$cursor = $collection->find()->sort(array( ' age'=>-1,'type'=>1)); ##1 According to descending order  -1 According to ascending order , The order of parameters affects the sort order 
/**  The index   **/
$collection->ensureIndex(array( ' age' => 1,'type'=>-1)); #1 According to descending order  -1 According to ascending order 
$collection->ensureIndex(array( ' age' => 1,'type'=>-1),array( ' background'=>true)); # Index creation is run in the background ( The default is to run synchronously )
$collection->ensureIndex(array( ' age' => 1,'type'=>-1),array( ' unique'=>true)); # The index is unique 1 the 
/**
* ensureIndex (array(),array( ' name'=>' The index name ','background'=true,'unique'=true))
*  As shown in the :http://www.php.net/manual/en/mongocollection.ensureindex.php
*/
/**  Get query results  **/
$cursor = $collection->find();
$array=array();
foreach ($cursor as $id => $value) {
$array[]=$value;
}
//*************************
//**    Document clustering      **//
//*************************
// This thing doesn't make sense... 
$conn->close(); # Close the connection 
/*
 Relational databases and MongoDB Differences in data storage 
MySql The data structure :
CREATE TABLE IF NOT EXISTS `column`(
`column_id` int(16)  NOT NULL  auto_increment  COMMENT  'the primary key ',
`column_name` varchar(32) NOT NULL COMMENT  Column Name ',
PRIMARY KEY  (`column_id`)
);
CREATE TABLE IF NOT EXISTS `article`(
`article_id`  int(16)  NOT NULL  auto_increment  COMMENT  'the primary key ',
`article_caption` varchar(15) NOT NULL COMMENT  'title ',
PRIMARY KEY(`article_id`)
);
CREATE TABLE IF NOT EXISTS `article_body`(
`article_id` int(16) NOT NULL COMMENT  ' article.article_id',
`body` text COMMENT  'the body '
);
MongoDB The data structure :
$data=array(
 ' column_name' =>'default',
 ' article' =>array(
 ' article_caption' =>  ' xiaocai',
 ' body'   =>  ' xxxxxxxxxx ... '
)
);
$inc
 If the recorded node exists, increment the value of the node N ; If the node does not exist, make the node value equal to N
 Let the structure record structure be  array('a'=>1,'b'=>'t'), Want to make a add 5 , then: 
$coll->update(
array('b'=>'t'),
array('$inc'=>array('a'=>5)),
)
$set
 Let a node be equal to a given value 
 Let the structure record structure be  array('a'=>1,'b'=>'t'),b To add f , then: 
$coll->update(
array('a'=>1),
array('$set'=>array('b'=>'f')),
)
$unset
 Delete a node 
 Let the record structure be  array('a'=>1,'b'=>'t') Want to delete, b Node, then: 
$coll->update(
array('a'=>1),
array('$unset'=>'b'),
)
$push
 If the corresponding node is an array, append 1 I'm going to go up to a new value; If it doesn't exist, create the array and attach it 1 The values are in this array; Returns an error if the node is not an array. 
 Let the record structure be array('a'=>array(0=>'haha'),'b'=& gt;1) , you want to attach new data to the node a , then: 
$coll->update(
array('b'=>1),
array('$push'=>array('a'=>'wow')),
)
 In this way, the record would be: array('a'=>array(0=>'haha',1=>'wow'),'b'=>1)
$pushAll
 with $push Similar, only yes 1 Subappends more than one value to a node 
$addToSet
 If there is no value in the array at that stage, add it 
 Let the record structure be array('a'=>array(0=& gt;'haha'),'b'=>1) , if you want to attach new data to the node a , then: 
$coll->update(
array('b'=>1),
array('$addToSet'=>array('a'=>'wow')),
)
 If the a It's already in the node wow, Then no new ones will be added; if not, new ones will be added for the node item - wow . 
$pop
 Set the record as array('a'=>array(0=>'haha',1=& gt;'wow'),'b'=>1)
 Deletes the end of an array node 1 An element :
$coll->update(
array('b'=>1),
array('$pop=>array('a'=>1)),
)
 Deletes the first phase of an array 1 An element 
$coll->update(
array('b'=>1),
array('$pop=>array('a'=>-1)),
)
$pull
 If the node is an array, delete its value as value Is returned if it is not an array 1 A mistake. 
 Set the record as  array('a'=>array(0=>'haha',1=>'wow'),'b'=>1) , want to delete a In the value for  haha The items: 
$coll->update(
array('b'=>1),
array('$pull=>array('a'=>'haha')),
)
 The result is:  array('a'=>array(0=>'wow'),'b'=>1)
$pullAll
 with $pull Similar, but can delete 1 Group of qualifying records. 
*/
?>


<?php
/**
* PHP operation MongoDB Learning notes 
*/
//*************************
//**    The connection MongoDB The database   **//
//*************************
// format =>( " mongodb:// The user name : password  @ address : port / Default specified database" , parameter )
$conn = new Mongo();
// We could write it as 
//$conn=new Mongo(); # Connect to localhost , The default port .
//$conn=new Mongo( " 172.21.15.69 " ); # Connect to remote host 
//$conn=new Mongo( " xiaocai.loc:10086 " ); # Connect to a remote host on the specified port 
//$conn=new Mongo( " xiaocai.loc " ,array( " replicaSet " =>true)); # Load balancing 
//$conn=new Mongo( " xiaocai.loc " ,array( " persist " => " t " )); # A persistent connection 
//$conn=new Mongo( " mongodb://sa:123@localhost " ); # Bring the username and password 
//$conn=new Mongo( " mongodb://localhost:27017,localhost:27018 " ); # Connect to multiple servers 
//$conn=new Mongo( " mongodb:///tmp/mongo-27017.sock " ); # Domain socket 
//$conn=new Mongo( " mongodb://admin_miss:miss@localhost:27017/test " ,array( ' persist'=>'p', " replicaSet " =>true)); # complete 
// Detailed information on :http://www.php.net/manual/en/mongo.connecting.php
//*************************
//**    Select database and table     **//
//*************************
$db=$conn->mydb; # choose mydb The database 
//$db=$conn->selectDB( " mydb " ); # The first 2 Kind of writing 
$collection=$db->column; # Choice set ( choose ' table ')
//$collection=$db->selectCollection( ' column'); # The first 2 Kind of writing 
//$collection=$conn->mydb->column; # Let me write it a little bit more succinctly 
// Pay attention to :1. Databases and collections do not need to be created beforehand , They are automatically created if they do not exist .
//   2. Watch out for typos , You might inadvertently create one 1 A new database ( With the original database chaos ).
//*************************
//**    Inserted into the document      **//
//*************************
//** Inserts data into the collection , return bool Determines whether the insert was successful . **/
$array=array( ' column_name'=>'col'.rand(100,999),'column_exp'=>'xiaocai');
$result=$collection->insert($array); # A simple insert 
echo  "The new record ID: " .$array['_id']; #MongoDB Returns the 1 Each record identification 
var_dump($result); # return :bool(true)
//** Securely insert data into the collection , Return to insert status ( An array of ). **/
$array=array( ' column_name'=>'col'.rand(100,999),'column_exp'=>'xiaocai2 ' );
$result=$collection->insert($array,true); # For waiting for MongoDB To complete the operation , To determine if it was successful .( It is useful to use this parameter when a large number of records are inserted )
echo  "The new record ID: " .$array['_id']; #MongoDB Returns the 1 Each record identification 
var_dump($result); # return :array(3) { ["err"]=> NULL ["n"]=> int(0) ["ok"]=> float(1) }
//** Let me write it in full  **/
#insert($array,array( ' safe'=>false,'fsync'=>false,'timeout'=>10000))
/*
* *
*  Full format :insert ( array $a [, array $options = array() ] )
*    insert(array(),array( ' safe'=>false,'fsync'=>false,'timeout'=>10000))
*        parameter :safe: The default false, Whether it is safe to write 
*   fsync: The default false, Whether to force insert into sync to disk 
*     timeout: timeout ( ms )
*
*  Insert the results :{  " _id "  : ObjectId( " 4d63552ad549a02c01000009 " ),  " column_name "  :  " col770 " ,  " column_exp "  :  " xiaocai "  }
*    '_id' Primary key field , The insert is MongoDB Automatically add .
*
*     Pay attention to :1. The following two inserts are the same 1 records ( The same _id), Because they have the same value. 
*         $collection->insert(array( ' column_name'=>'xiaocai'));
*         $collection->insert(array( ' column_name'=>'xiaocai'));
*      avoid 
* $collection->insert(array( ' column_name'=>'xiaocai'),true);
* try {
*      $collection->insert(array( ' column_name'=>'xiaocai'),true);
* }catch(MongoCursorException $e){
*      echo  " Can't save the same person twice!\n " ;
* }
*
*     Detailed information on :http://www.php.net/manual/zh/mongocollection.insert.php
* *
*/
//*************************
//**    Update the document      **//
//*************************
//**  Modify the update  **/
$where=array( ' column_name'=>'col123 ' );
$newdata=array( ' column_exp'=>'GGGGGGG','column_fid'=>444);
$result=$collection->update($where,array( ' $set'=>$newdata)); #$set: Let a node be equal to a given value , Similar to that $pull $pullAll $pop $inc, Use is explained slowly later 
/*
*  The results of :
*  The original data 
* { " _id " :ObjectId( " 4d635ba2d549a02801000003 " ), " column_name " : " col123 " , " column_exp " : " xiaocai " }
*  Replaced by 
* { " _id " :ObjectId( " 4d635ba2d549a02801000003 " ), " column_name " : " col123 " , " column_exp " : " GGGGGGG " , " column_fid " :444}
*/
//**  Replace the update  **/
$where=array( ' column_name'=>'col709 ' );
$newdata=array( ' column_exp'=>'HHHHHHHHH','column_fid'=>123);
$result=$collection->update($where,$newdata);
/*
*  The results of :
*  The original data 
* { " _id " :ObjectId( " 4d635ba2d549a02801000003 " ), " column_name " : " col709 " , " column_exp " : " xiaocai " }
*  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 
*/
//**  Automatic accumulation  **/
$where=array('91u'=>684435);
$newdata=array( ' column_exp'=>'edit');
$result=$collection->update($where,array( ' $set'=>$newdata,'$inc'=>array('91u'=>-5)));
/**
*  update 91u=684435 The data of , and 91u Since the reduction of 5
*/
/**  Remove nodes  **/
$where=array( ' column_name'=>'col685 ' );
$result=$collection->update($where,array( ' $unset'=>'column_exp'));
/**
*  Remove nodes column_exp
*/
/*
* *
*  Full format :update(array $criteria, array $newobj [, array $options = array()  ] )
*        Pay attention to :1. Note the distinction between replacement updates and modification updates 
*    2. Note the distinction between data types such as  array('91u'=>'684435 ' ) with array('91u'=>684435)
*  Detailed information on :http://www.mongodb.org/display/DOCS/Updating#Updating-%24bit
* *
*/
//*************************
//**    Delete the document      **//
//*************************
/**  Clear the database  **/
$collection->remove(array( ' column_name'=>'col399 ' ));
//$collection->remove(); # Empty the collection 
/**  Delete the specified MongoId **/
$id = new MongoId( " 4d638ea1d549a02801000011 " );
$collection->remove(array( ' _id'=>(object)$id));
/*
* *
*   Use the following method to match { " _id " :ObjectId( " 4d638ea1d549a02801000011 " )}, Query, update also 1 sample 
*  $id = new MongoId( " 4d638ea1d549a02801000011 " );
*  array( ' _id'=>(object)$id)
* *
*/
//*************************
//**    Query the document      **//
//*************************
/**  Query the number of records in the document  **/
echo  ' count:'.$collection->count(). " <br> " ; # all 
echo  ' count:'.$collection->count(array( ' type'=>'user')). " <br> " ; # You can add conditions 
echo  ' count:'.$collection->count(array( ' age'=>array( ' $gt'=>50,'$lte'=>74))). " <br> " ; # Is greater than 50 Less than or equal to 74
echo  ' count:'.$collection->find()->limit(5)->skip(0)->count(true). " <br> " ; # Gets the actual number of results returned 
/**
*  note :$gt For more than, $gte Is greater than or equal to, $lt For less than, $lte Is less than or equal to, $ne Is not equal to, $exists There is no 
*/
/**  All documents in the collection  **/
$cursor = $collection->find()->snapshot();
foreach ($cursor as $id => $value) {
echo  " $id:  " ; var_dump($value); echo  " <br> " ;
}
/**
*  Pay attention to :
*  Before we did find() Operate, obtain $cursor After the cursor, the cursor is still dynamic .
*  In other words, , In my find() after , The loop to my cursor completes this time , If any more qualifying records are inserted into the collection, Then these records will also be $cursor  To obtain .
*  If you want to get in $cursor The subsequent result set does not change , Here's what you need to do: 
* $cursor = $collection->find();
* $cursor->snapshot();
*  As shown in the http://www.bumao.com/index.php/2010/08/mongo_php_cursor.html
*/
/**  The query 1 The data  **/
$cursor = $collection->findOne();
/**
*   Pay attention to :findOne() Cannot be used after obtaining the result set snapshot(),fields() Such as function ;
*/
/** age,type  Columns do not display  **/
$cursor = $collection->find()->fields(array( " age " =>false, " type " =>false));
/**  Display only user  column  **/
$cursor = $collection->find()->fields(array( " user " =>true));
/**
*  I'm going to make a mistake writing it this way :$cursor->fields(array( " age " =>true, " type " =>false));
*/
/** ( There are 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 gets the result set   **/
$cursor = $collection->find()->limit(5)->skip(0);
/**  The sorting   **/
$cursor = $collection->find()->sort(array( ' age'=>-1,'type'=>1)); ##1 According to descending order  -1 According to ascending order , The order of parameters affects the sort order 
/**  The index   **/
$collection->ensureIndex(array( ' age' => 1,'type'=>-1)); #1 According to descending order  -1 According to ascending order 
$collection->ensureIndex(array( ' age' => 1,'type'=>-1),array( ' background'=>true)); # Index creation is run in the background ( The default is to run synchronously )
$collection->ensureIndex(array( ' age' => 1,'type'=>-1),array( ' unique'=>true)); # The index is unique 1 the 
/**
* ensureIndex (array(),array( ' name'=>' The index name ','background'=true,'unique'=true))
*  As shown in the :http://www.php.net/manual/en/mongocollection.ensureindex.php
*/
/**  Get query results  **/
$cursor = $collection->find();
$array=array();
foreach ($cursor as $id => $value) {
$array[]=$value;
}
//*************************
//**    Document clustering      **//
//*************************
// This thing doesn't make sense... 
$conn->close(); # Close the connection 
/*
 Relational databases and MongoDB Differences in data storage 
MySql data   structure :
CREATE TABLE IF NOT EXISTS `column`(
`column_id` int(16)  NOT NULL  auto_increment  COMMENT  'the primary key ',
`column_name` varchar(32) NOT NULL COMMENT  Column Name ',
PRIMARY KEY  (`column_id`)
);
CREATE TABLE IF NOT EXISTS `article`(
`article_id`  int(16)  NOT NULL  auto_increment  COMMENT  'the primary key ',
`article_caption` varchar(15) NOT NULL COMMENT  'title ',
PRIMARY KEY(`article_id`)
);
CREATE TABLE IF NOT EXISTS `article_body`(
`article_id` int(16) NOT NULL COMMENT  ' article.article_id',
`body` text COMMENT  'the body '
);
MongoDB The data structure :
$data=array(
 ' column_name' =>'default',
 ' article' =>array(
 ' article_caption' =>  ' xiaocai',
 ' body'   =>  ' xxxxxxxxxx ... '
)
);
$inc
 If the recorded node exists, increment the value of the node N ; If the node does not exist, let the node value and so on   in N
 Let the structure record structure be  array('a'=>1,'b'=>'t'), Want to make a add 5 , then: 
$coll->update(
array('b'=>'t'),
array('$inc'=>array('a'=>5)),
)
$set
 Let a node be equal to a given value 
 Let the structure record structure be  array('a'=>1,'b'=>'t'),b To add f , then: 
$coll->update(
array('a'=>1),
array('$set'=>array('b'=>'f')),
)
$unset
 Delete a node 
 Let the record structure be  array('a'=>1,'b'=>'t') Want to delete, b Node, then: 
$coll->update(
array('a'=>1),
array('$unset'=>'b'),
)
$push
 If the corresponding node is an array, append 1 I'm going to go up to a new value; If it doesn't exist, create the array and attach it 1 The values are in this array; if   This node is not an array and returns an error. 
 Let the record structure be array('a'=>array(0=>'haha'),'b'=& gt;1) , you want to attach new data to the node a , then: 
$coll->update(
array('b'=>1),
array('$push'=>array('a'=>'wow')),
)
 this   The record would then be: array('a'=>array(0=>'haha',1=>'wow'),'b'=>1)
$pushAll
 with $push Similar, only yes 1 Subappends more than one value to a node 
$addToSet
 If there is no value in the array at that stage, add it 
 Let the record structure be array('a'=>array(0=& gt;'haha'),'b'=>1) , if you want to attach new data to the node a , then: 
$coll->update(
array('b'=>1),
array('$addToSet'=>array('a'=>'wow')),
)
 If the a It's already in the node wow, Then no new ones will be added; if not, new ones will be added for the node item - wow . 
$pop
 Set the record as array('a'=>array(0=>'haha',1=& gt;'wow'),'b'=>1)
 Deletes the end of an array node 1 An element :
$coll->update(
array('b'=>1),
array('$pop=>array('a'=>1)),
)
 Deletes the first phase of an array 1 An element 
$coll->update(
array('b'=>1),
array('$pop=>array('a'=>-1)),
)
$pull
 If the node is an array, delete its value as value Is returned if it is not an array 1 A mistake. 
 Set the record as  array('a'=>array(0=>'haha',1=>'wow'),'b'=>1) , want to delete a In the value for  haha The items: 
$coll->update(
array('b'=>1),
array('$pull=>array('a'=>'haha')),
)
 "   Fruit is:  array('a'=>array(0=>'wow'),'b'=>1)
$pullAll
 with $pull Similar, but can delete 1 Group of qualifying records. 
*/
?>


Related articles: