code show as below:
<? php
/ **
* PHP operation MongoDB study notes
* /
// *************************
// ** connect to MongoDB database ** //
// *************************
// format => ("mongodb: // username: password @ address: port / default-designated database", parameter)
$ conn = new Mongo ();
// can be abbreviated as
// $ conn = new Mongo (); #Connect to localhost, default port.
// $ conn = new Mongo (“172.21.15.69 ″); #Connect to a remote host
// $ conn = new Mongo (“xiaocai.loc: 10086 ″); #connect to the remote host with 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
// $ conn = new Mongo ("mongodb: // localhost: 27017, localhost: 27018 ″); #connect 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
// Details: http://www.php.net/manual/en/mongo.connecting.php
// *************************
// ** Select database and table ** //
// *************************
$ db = $ conn-> mydb; #Select mydb database
// $ db = $ conn-> selectDB ("mydb"); #The second way of writing
$ collection = $ db-> column; #Select collection (select 'table')
// $ collection = $ db-> selectCollection (‘column '); #second writing
// $ collection = $ conn-> mydb-> column; #More concise writing
// Note: 1.Databases and collections do not need to be created in advance, if they do not exist, they will be created automatically.
// 2. Pay attention to typos, you may accidentally create a new database (confusion with the original database).
// *************************
// ** insert document ** //
// *************************
// ** Insert data into the collection and return bool to determine whether the insertion was successful. ** /
$ array = array (‘column_name '=>' col'.rand (100,999), 'column_exp' => 'xiaocai');
$ result = $ collection-> insert ($ array); #Simple insert
echo "new record ID:". $ array ['_ id']; #MongoDB will return a record ID
var_dump ($ result); #Return: bool (true)
// ** Safely insert data into the collection and return the insertion status (array). ** /
$ array = array (‘column_name '=>' col'.rand (100,999), 'column_exp' => 'xiaocai2 ′);
$ result = $ collection-> insert ($ array, true); #It is used to wait for MongoDB to complete the operation in order to determine whether it is successful. (This parameter is more useful when there are a large number of records inserted)
echo "new record ID:". $ array ['_ id']; #MongoDB will return a record ID
var_dump ($ result); #Return: array (3) {["err"] => NULL ["n"] => int (0) ["ok"] => float (1)}
// ** complete writing ** /
#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: default false, whether to write safely
* fsync: false by default, whether to force insert to sync to disk
* timeout: timeout (ms)
*
* Insert result: {“_id”: ObjectId (“4d63552ad549a02c01000009 ″),“ column_name ”:“ col770 ”,“ column_exp ”:“ xiaocai ”}
* '_id' is the primary key field, which is automatically added by MongoDB during insertion.
*
* Note: 1. The following two inserts are the same record (same _id), because their values are the same.
* $ 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";
*}
*
* Details: http://www.php.net/manual/zh/mongocollection.insert.php
* *
* /
// *************************
// ** update documentation ** //
// *************************
// ** modify update ** /
$ where = array (‘column_name '=>' col123 ′);
$ newdata = array (‘column_exp '=>' GGGGGGG ',' column_fid '=> 444);
$ result = $ collection-> update ($ where, array ('$ set' => $ newdata)); # $ set: Make a node equal to a given value, similarly $ pull $ pullAll $ pop $ inc, Use slowly later
/ *
* Results:
* raw data
* {"_Id": ObjectId ("4d635ba2d549a02801000003 ″)," column_name ":" col123 "," column_exp ":" xiaocai "}
* Replaced with
* {"_Id": ObjectId ("4d635ba2d549a02801000003 ″)," column_name ":" col123 "," column_exp ":" GGGGGGG "," column_fid ": 444}
* /
// ** replace update ** /
$ where = array (‘column_name '=>' col709 ′);
$ newdata = array (‘column_exp '=>' HHHHHHHHHH ',' column_fid '=> 123);
$ result = $ collection-> update ($ where, $ newdata);
/ *
* Results:
* raw data
* {"_Id": ObjectId ("4d635ba2d549a02801000003 ″)," column_name ":" col709 "," column_exp ":" xiaocai "}
* Replaced with
* {"_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' is modified
* /
// ** auto accumulation ** /
$ where = array ('91u' => 684435);
$ newdata = array (‘column_exp '=>' edit ');
$ result = $ collection-> update ($ where, array (‘$ set '=> $ newdata,' $ inc '=> array (' 91u '=>-5)));
/ **
* Update the data of 91u = 684435, and decrease 91u by 5
* /
/ ** 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 the distinction between replacement update and modification update
* 2. Note the distinction between data types such as array ('91u' => '684435') and array ('91u' => 684435)
* Details: http://www.mongodb.org/display/DOCS/Updating#Updating-%24bit
* *
* /
// *************************
// ** delete document ** //
// *************************
/ ** Clear 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 ″)}, as well as query and update
* $ id = new MongoId ("4d638ea1d549a02801000011 ″);
* array (‘_ id '=> (object) $ id)
* *
* /
// *************************
// ** query document ** //
// *************************
/ ** Query the number of records in the document** /
echo ‘count: '. $ collection-> count ().” <br> ”; #All
echo ‘count: '. $ collection-> count (array (‘ type' => 'user')). ”<br>”; #Can add conditions
echo 'count:'. $ collection-> count (array ('age' => array ('$ gt' => 50, '$ lte' => 74))). "<br>"; #greater than Equal to 74
echo ‘count: '. $ collection-> find ()-> limit (5)-> skip (0)-> count (true).” <br> ”; #Get the actual number of results returned
/ **
* Note: $ gt is greater than, $ gte is greater than or equal to, $ lt is less than or equal to, $ lte is less than or equal to, $ ne is not equal to, and $ exists does not exist
* /
/ ** All documents in the collection ** /
$ cursor = $ collection-> find ()-> snapshot ();
foreach ($ cursor as $ id => $ value) {
echo "$ id:"; var_dump ($ value); echo "<br>";
}
/ **
* Note:
* After we did find () operation and obtained $ cursor cursor, the cursor is still dynamic.
* In other words, after I find (), until my cursor cycle is completed, if there are any more eligible records inserted into the collection, these records will also be obtained by $ cursor.
* If you want the result set to remain unchanged after you get $ cursor, you need to do this:
* $ cursor = $ collection-> find ();
* $ cursor-> snapshot ();
* See http://www.bumao.com/index.php/2010/08/mongo_php_cursor.html for details
* /
/ ** Query a piece of data ** /
$ cursor = $ collection-> findOne ();
/ **
* Note: After findOne () obtains the result set, you cannot use snapshot (), fields () and other functions;
* /
/ ** age, type columns do not show ** /
$ cursor = $ collection-> find ()-> fields (array ("age" => false, "type" => false));
/ ** Show only the user column ** /
$ cursor = $ collection-> find ()-> fields (array ("user" => true));
/ **
* I will make an error writing this: $ cursor-> fields (array (“age” => true, ”type” => false));
* /
/ ** (exist 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);
/ ** Paginating the result set ** /
$ cursor = $ collection-> find ()-> limit (5)-> skip (0);
/ ** Sort ** /
$ cursor = $ collection-> find ()-> sort (array (‘age '=>-1,' type '=> 1)); ## 1 indicates descending order -1 indicates ascending order, the order of the parameters affects the sort order
/ ** index ** /
$ collection-> ensureIndex (array (‘age '=> 1,' type '=>-1)); # 1 means descending order -1 means ascending order
$ collection-> ensureIndex (array (‘age '=> 1,' type '=>-1), array (‘ background' => true)); #The creation of the index 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
/ **
* 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;
}
// *************************
// ** document clustering ** //
// *************************
// I didn't figure it out ...
$ conn-> close (); #Close the connection
/ *
Difference between relational database and MongoDB data storage
MySql data structure:
CREATE TABLE IF NOT EXISTS `column` (
`column_id` int (16) NOT NULL auto_increment COMMENT‘ 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‘ 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
);
MongoDB data structure:
$ data = array (
‘Column_name '=>' default ',
‘Article '=> array (
‘Article_caption '=>‘ xiaocai',
‘Body '=>‘ xxxxxxxxxx ...'
)
);
$ inc
If the recorded node exists, let the value of the node increase by N; if the node does not exist, let the value of the node equal to N
Let the structure record structure be array ('a' => 1, 'b' => 't'), and you want to increase a by 5, then:
$ coll-> update (
array ('b' => 't'),
array ('$ inc' => array ('a' => 5)),
)
$ set
Make a node equal to a given value
Let the structure record structure be array ('a' => 1, 'b' => 't'), and b is plus 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'). If you want to delete the b node, then:
$ coll-> update (
array ('a' => 1),
array ('$ unset' => 'b'),
)
$ push
If the corresponding node is an array, a new value is appended; if it does not exist, the array is created and a value is appended to the array; if the node is not an array, an error is returned.
Let the record structure be array ('a' => array (0 => 'haha'), 'b' = & gt; 1), and you want to append new data to node a, then:
$ coll-> update (
array ('b' => 1),
array ('$ push' => array ('a' => 'wow')),
)
In this way, the record becomes: array ('a' => array (0 => 'haha', 1 => 'wow'), 'b' => 1)
$ pushAll
Similar to $ push, except that multiple values will be appended to a node at a time
$ addToSet
If there is no value in the array at this stage, add it
Let the record structure be array ('a' => array (0 = & 'gt;' haha '),' b '=> 1). If you want to append new data to the node a, then:
$ coll-> update (
array ('b' => 1),
array ('$ addToSet' => array ('a' => 'wow')),
)
If there is already a wow in the a node, then no new one will be added, if not, a new item-wow will be added for the node.
$ pop
Let this record be array ('a' => array (0 => 'haha', 1 = & gt; 'wow'), 'b' => 1)
Remove the last element of an array node:
$ coll-> update (
array ('b' => 1),
array ('$ pop => array (' a '=> 1)),
)
Delete the first element of an array stage
$ coll-> update (
array ('b' => 1),
array ('$ pop => array (' a '=>-1)),
)
$ pull
If the node is an array, delete the child whose value is value. If it is not an array, an error is returned.
Let this record be array ('a' => array (0 => 'haha', 1 => 'wow'), 'b' => 1), and you want to delete the child whose value is haha:
$ coll-> update (
array ('b' => 1),
array ('$ pull => array (' a '=>' haha ')),
)
The result is: array ('a' => array (0 => 'wow'), 'b' => 1)
$ pullAll
Similar to $ pull, except that you can delete a set of eligible records.
* /
?>
Copy the code:
<? php
/ **
* PHP operation MongoDB study notes
* /
// *************************
// ** connect to MongoDB database ** //
// *************************
// format => ("mongodb: // username: password @ address: port / default-designated database", parameter)
$ conn = new Mongo ();
// can be abbreviated as
// $ conn = new Mongo (); #Connect to localhost, default port.
// $ conn = new Mongo (“172.21.15.69 ″); #Connect to a remote host
// $ conn = new Mongo (“xiaocai.loc: 10086 ″); #connect to the remote host with 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
// $ conn = new Mongo ("mongodb: // localhost: 27017, localhost: 27018 ″); #connect 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
// Details: http://www.php.net/manual/en/mongo.connecting.php
// *************************
// ** Select database and table ** //
// *************************
$ db = $ conn-> mydb; #Select mydb database
// $ db = $ conn-> selectDB ("mydb"); #The second way of writing
$ collection = $ db-> column; #Select collection (select 'table')
// $ collection = $ db-> selectCollection (‘column '); #second writing
// $ collection = $ conn-> mydb-> column; #More concise writing
// Note: 1.Databases and collections do not need to be created in advance, if they do not exist, they will be created automatically.
// 2. Pay attention to typos, you may accidentally create a new database (confusion with the original database).
// *************************
// ** insert document ** //
// *************************
// ** Insert data into the collection and return bool to determine whether the insertion was successful. ** /
$ array = array (‘column_name '=>' col'.rand (100,999), 'column_exp' => 'xiaocai');
$ result = $ collection-> insert ($ array); #Simple insert
echo "new record ID:". $ array ['_ id']; #MongoDB will return a record ID
var_dump ($ result); #Return: bool (true)
// ** Safely insert data into the collection and return the insertion status (array). ** /
$ array = array (‘column_name '=>' col'.rand (100,999), 'column_exp' => 'xiaocai2 ′);
$ result = $ collection-> insert ($ array, true); #It is used to wait for MongoDB to complete the operation in order to determine whether it is successful. (This parameter is more useful when there are a large number of records inserted)
echo "new record ID:". $ array ['_ id']; #MongoDB will return a record ID
var_dump ($ result); #Return: array (3) {["err"] => NULL ["n"] => int (0) ["ok"] => float (1)}
// ** complete writing ** /
#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: default false, whether to write safely
* fsync: false by default, whether to force insert to sync to disk
* timeout: timeout (ms)
*
* Insert result: {“_id”: ObjectId (“4d63552ad549a02c01000009 ″),“ column_name ”:“ col770 ”,“ column_exp ”:“ xiaocai ”}
* '_id' is the primary key field, which is automatically added by MongoDB during insertion.
*
* Note: 1. The following two inserts are the same record (same _id), because their values are the same.
* $ 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";
*}
*
* Details: http://www.php.net/manual/zh/mongocollection.insert.php
* *
* /
// *************************
// ** update documentation ** //
// *************************
// ** modify update ** /
$ where = array (‘column_name '=>' col123 ′);
$ newdata = array (‘column_exp '=>' GGGGGGG ',' column_fid '=> 444);
$ result = $ collection-> update ($ where, array ('$ set' => $ newdata)); # $ set: Make a node equal to a given value, similarly $ pull $ pullAll $ pop $ inc, Use slowly later
/ *
* Results:
* raw data
* {"_Id": ObjectId ("4d635ba2d549a02801000003 ″)," column_name ":" col123 "," column_exp ":" xiaocai "}
* Replaced with
* {"_Id": ObjectId ("4d635ba2d549a02801000003 ″)," column_name ":" col123 "," column_exp ":" GGGGGGG "," column_fid ": 444}
* /
// ** replace update ** /
$ where = array (‘column_name '=>' col709 ′);
$ newdata = array (‘column_exp '=>' HHHHHHHHHH ',' column_fid '=> 123);
$ result = $ collection-> update ($ where, $ newdata);
/ *
* Results:
* raw data
* {"_Id": ObjectId ("4d635ba2d549a02801000003 ″)," column_name ":" col709 "," column_exp ":" xiaocai "}
* Replaced with
* {"_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' is modified
* /
// ** auto accumulation ** /
$ where = array ('91u' => 684435);
$ newdata = array (‘column_exp '=>' edit ');
$ result = $ collection-> update ($ where, array (‘$ set '=> $ newdata,' $ inc '=> array (' 91u '=>-5)));
/ **
* Update the data of 91u = 684435, and decrease 91u by 5
* /
/ ** 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 the distinction between replacement update and modification update
* 2. Note the distinction between data types such as array ('91u' => '684435') and array ('91u' => 684435)
* Details: http://www.mongodb.org/display/DOCS/Updating#Updating-%24bit
* *
* /
// *************************
// ** delete document ** //
// *************************
/ ** Clear 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 ″)}, as well as query and update
* $ id = new MongoId ("4d638ea1d549a02801000011 ″);
* array (‘_ id '=> (object) $ id)
* *
* /
// *************************
// ** query document ** //
// *************************
/ ** Query the number of records in the document ** /
echo ‘count: '. $ collection-> count ().” <br> ”; #All
echo ‘count: '. $ collection-> count (array (‘ type' => 'user')). ”<br>”; #Can add conditions
echo 'count:'. $ collection-> count (array ('age' => array ('$ gt' => 50, '$ lte' => 74))). "<br>"; #greater than Equal to 74
echo ‘count: '. $ collection-> find ()-> limit (5)-> skip (0)-> count (true).” <br> ”; #Get the actual number of results returned
/ **
* Note: $ gt is greater than, $ gte is greater than or equal to, $ lt is less than or equal to, $ lte is less than or equal to, $ ne is not equal to, and $ exists does not exist
* /
/ ** All documents in the collection ** /
$ cursor = $ collection-> find ()-> snapshot ();
foreach ($ cursor as $ id => $ value) {
echo "$ id:"; var_dump ($ value); echo "<br>";
}
/ **
* Note:
* After we did find () operation and obtained $ cursor cursor, the cursor is still dynamic.
* In other words, after I find (), until my cursor cycle is completed, if there are any more eligible records inserted into the collection, these records will also be obtained by $ cursor.
* If you want the result set to remain unchanged after you get $ cursor, you need to do this:
* $ cursor = $ collection-> find ();
* $ cursor-> snapshot ();
* See http://www.bumao.com/index.php/2010/08/mongo_php_cursor.html for details
* /
/ ** Query a piece of data ** /
$ cursor = $ collection-> findOne ();
/ **
* Note: After findOne () obtains the result set, you cannot use snapshot (), fields () and other functions;
* /
/ ** age, type columns do not show ** /
$ cursor = $ collection-> find ()-> fields (array ("age" => false, "type" => false));
/ ** Show only the user column ** /
$ cursor = $ collection-> find ()-> fields (array ("user" => true));
/ **
* I will make an error writing this: $ cursor-> fields (array (“age” => true, ”type” => false));
* /
/ ** (exist 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);
/ ** Paginating the result set ** /
$ cursor = $ collection-> find ()-> limit (5)-> skip (0);
/ ** Sort ** /
$ cursor = $ collection-> find ()-> sort (array (‘age '=>-1,' type '=> 1)); ## 1 indicates descending order -1 indicates ascending order, the order of the parameters affects the sort order
/ ** index ** /
$ collection-> ensureIndex (array (‘age '=> 1,' type '=>-1)); # 1 means descending order -1 means ascending order
$ collection-> ensureIndex (array (‘age '=> 1,' type '=>-1), array (‘ background' => true)); #The creation of the index 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
/ **
* 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;
}
// *************************
// ** document clustering ** //
// *************************
// I didn't figure it out ...
$ conn-> close (); #Close the connection
/ *
Difference between relational database and MongoDB data storage
MySql data structure:
CREATE TABLE IF NOT EXISTS `column` (
`column_id` int (16) NOT NULL auto_increment COMMENT‘ 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‘ 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
);
MongoDB data structure:
$ data = array (
‘Column_name '=>' default ',
‘Article '=> array (
‘Article_caption '=>‘ xiaocai',
‘Body '=>‘ xxxxxxxxxx ...'
)
);
$ inc
If the recorded node exists, increase the value of the node by N; if the node does not exist, let the value of the node equal to N
Let the structure record structure be array ('a' => 1, 'b' => 't'), and you want to increase a by 5, then:
$ coll-> update (
array ('b' => 't'),
array ('$ inc' => array ('a' => 5)),
)
$ set
Make a node equal to a given value
Let the structure record structure be array ('a' => 1, 'b' => 't'), and b is plus 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'). If you want to delete the b node, then:
$ coll-> update (
array ('a' => 1),
array ('$ unset' => 'b'),
)
$ push
If the corresponding node is an array, a new value is appended; if it does not exist, the array is created and a value is appended to the array; if the node is not an array, an error is returned.
Let the record structure be array ('a' => array (0 => 'haha'), 'b' = & gt; 1), and you want to append new data to node a, then:
$ coll-> update (
array ('b' => 1),
array ('$ push' => array ('a' => 'wow')),
)
This way, the record becomes: array ('a' => array (0 => 'haha', 1 => 'wow'), 'b' => 1)
$ pushAll
Similar to $ push, except that multiple values will be appended to a node at a time
$ addToSet
If there is no value in the array at this stage, add it
Let the record structure be array ('a' => array (0 = & 'gt;' haha '),' b '=> 1). If you want to append new data to the node a, then:
$ coll-> update (
array ('b' => 1),
array ('$ addToSet' => array ('a' => 'wow')),
)
If there is already a wow in the a node, then no new one will be added, if not, a new item-wow will be added for the node.
$ pop
Let this record be array ('a' => array (0 => 'haha', 1 = & gt; 'wow'), 'b' => 1)
Remove the last element of an array node:
$ coll-> update (
array ('b' => 1),
array ('$ pop => array (' a '=> 1)),
)
Delete the first element of an array stage
$ coll-> update (
array ('b' => 1),
array ('$ pop => array (' a '=>-1)),
)
$ pull
If the node is an array, delete the child whose value is value. If it is not an array, an error is returned.
Let this record be array ('a' => array (0 => 'haha', 1 => 'wow'), 'b' => 1), and you want to delete the child whose value is haha:
$ coll-> update (
array ('b' => 1),
array ('$ pull => array (' a '=>' haha ')),
)
The result is: array ('a' => array (0 => 'wow'), 'b' => 1)
$ pullAll
Similar to $ pull, except that you can delete a set of eligible records.
* /
?>