: This article mainly introduces the mapreduce usage and php sample code of mongodb. if you are interested in the PHP Tutorial, refer to it. Although MongoDB is not as easy to group as mysql, SQL Server, oracle, and other relational databases we use the group by function, there are three ways to implement grouping for MongoDB:
* Mongodb grouping methods:
* 1. group)
* 2. mapreduce (based on the js engine, single-thread execution, low efficiency, suitable for background statistics, etc)
* 3. aggregate (recommended, but 1.3 does not currently support account authentication mode, you can view updates and bugs through the http://pecl.php.net/package/mongo)
The following describes the mapreduce method:
Official Mongodb website introduces MapReduce:
Map/reduce in MongoDB is useful for batch processing of data and aggregation operations. it is similar in spirit to using something like Hadoop with all input coming from a collection and output going to a collection. often, in a situation where you wowould have used group by in SQL, map/reduce is the right tool in MongoDB.
In general, Map/reduce in Mongodb is mainly used for batch processing and aggregation of data. it is a bit similar to using Hadoop to process set data. all input data is obtained from the set, the data output after MapReduce is also written into the collection. It is usually similar to using the Group By statement in SQL.
Using MapReduce requires two functions: Map and Reduce. The Map function calls emit (key, value) to traverse all the records in the set and pass the key and value to the Reduce function for processing. Map and Reduce functions are written in Javascript and can be executed using db. runCommand or mapreduce commands.
The MapReduce command is as follows:
Db. runCommand ({mapreduce: , Map: , Reduce: [, Query: ] [, Sort: ] [, Limit: ] [, Out: ] [, Keeptemp: ] [, Finalize: ] [, Scope: ] [, Verbose: true]});
Parameter description:
Mapreduce: Target set to be operated
Map: ing function (generate a sequence of key-value pairs as a parameter of the Reduce function)
Reduce: statistical function
Query: target record filtering
Sort: sorting target Records
Limit: limit the number of target Records
Out: stores the statistical result set (if not specified, a temporary set is used and is automatically deleted after the client is disconnected)
Keeptemp: whether to retain the temporary set
Finalize: final processing function (execute final sorting on the result returned by reduce and save it to the result set)
Scope: import external variables to map, reduce, and finalize
Verbose: displays detailed time statistics
Map functions
The map function calls the current object, and transmits the object attributes to reduce. The map method uses this to operate the current object. emit (key, value) is called at least once) method to provide parameters to reduce. the key of emit is the id of the final data.
Reduce function
Receives a value and an array, and combines and groups the array as needed. The reduce key is the key of emit (key, value, value_array is an array of multiple values corresponding to the same key.
Finalize function
This function is an optional function that can be executed after map and reduce operations are completed, and the final data is processed in a unified manner.
After reading the basic introduction, let's look at an instance:
The test data is as follows:
{"_ Id": ObjectId ("50ccb3f91e937e2927000004"), "feed_type": 1, "to_user": 234, "time_line": "01:26:00"} {"_ id ": objectId ("50ccb3ef1e937e0727000004"), "feed_type": 8, "to_user": 123, "time_line": "01:26:00"} {"_ id ": objectId ("50ccb3e31e937e0a27000003"), "feed_type": 1, "to_user": 123, "time_line": "01:26:00"} {"_ id ": objectId ("50ccb3d31e937e0927000001"), "feed_type": 1, "to_user": 123, "time_line": "01:26:00 "}
We perform grouping statistics by dynamic feed_type and user to_user. The result is as follows:
| Feed_type |
To_user |
Cout |
| 1 |
234 |
1 |
| 8 |
123 |
1 |
| 1 |
123 |
2 |
Implementation code:
// Compile the map function $ map = 'Function () {var key = {to_user: this. to_user, feed_type: this. feed_type}; var value = {count: 1}; emit (key, value);} '; // reduce function $ reduce = 'Function (key, values) {var ret = {count: 0}; for (var I in values) {ret. count + = 1;} return ret;} '; // query condition $ query = null; // no query condition exists in this instance and is set to null. $ Mongo = new Mongo ('mongodb: // root: root@127.0.0.1: 28017/'); // Link to mongodb, the account and password are root, root $ instance = $ mongo-> selectDB ("testdb"); // after executing this command, a temporary set of feed_temp_res is created, place the statistical data in the collection $ cmd = $ instance-> command (array ('mapreduce' => 'feed', 'map' => $ map, 'reduce' => $ reduce, 'query' => $ query, 'out' => 'Feed _ temp_res '); // query the statistics in the temporary set, verify that the statistical results are consistent with the expected results. $ cursor = $ instance-> selectCollection ('feed _ temp_res ')-> find (); $ result = array (); try {while ($ cursor-> hasNext () {$ result [] = $ cursor-> getNext () ;}} catch (MongoConnectionException $ e) {echo $ e-> getMessage ();} catch (response cursortimeoutexception $ e) {echo $ e-> getMessage ();} catch (Exception $ e) {echo $ e-> getMessage ();} // testvar_dump ($ result );
The following is the output result, which is consistent with the expected result.
{"_ Id": {"to_user": 234, "feed_type": 1}, "value": {"count": 1 }{ "_ id ": {"to_user": 123, "feed_type": 8}, "value": {"count": 1 }}{ "_ id": {"to_user": 123, "feed_type": 1}, "value": {"count": 2 }}
The preceding is just a simple statistical implementation. you can compile complex reduce functions for complex conditional statistics, and add query conditions and sorting.
Attached mapReduce database processing functions (simple encapsulation)
/*** MapReduce grouping ** @ param string $ table_name table name (name of the target set to be operated) * @ param string $ map ing function (generate a sequence of key-value pairs, as a reduce function parameter) * @ param string $ reduce statistical processing function * @ param array $ query filtering condition, for example, array ('uid' => 123) * @ param array $ sort sorting * @ param number $ number of target records restricted by limit * @ param string $ out the set of statistical results (if not specified, tmp_mr_res _ $ table_name, 1.8 or a later version must be specified) * @ param bool $ keeptemp whether to retain the temporary set * @ param string $ finalize final processing function (final processing of the result returned by reduce) After sorting and storing the result set) * @ param string $ scope imports external js variables to map, reduce, and finalize * @ param bool $ jsMode whether to reduce the BSON and JS conversions during execution, the default value is true (note: If it is false, BSON --> JS --> map --> BSON --> JS --> reduce --> BSON can process very large mapreduce, // if it is true, BSON --> js --> map --> reduce --> BSON) * @ param bool $ whether verbose generates more detailed server logs * @ param bool $ whether returnresult returns a new result set * @ param array & $ cmdresult returns the mp command execution result array (" errmsg "=> "", "code" => 13606, "OK" => 0) OK = 1 indicates that the command is successfully executed * @ retu Rn */function mapReduce ($ table_name, $ map, $ reduce, $ query = null, $ sort = null, $ limit = 0, $ out = '', $ keeptemp = true, $ finalize = null, $ scope = null, $ jsMode = true, $ verbose = true, $ returnresult = true, & $ cmdresult) {if (empty ($ table_name) | empty ($ map) | empty ($ reduce) {return null;} $ map = new MongoCode ($ map ); $ reduce = new reduce code ($ reduce); if (empty ($ out) {$ out = 'tmp _ mr_res _'. $ table_name;} $ cmd = array ('mapredu Ce '=> $ table_name, 'map' => $ map, 'reduce' => $ reduce, 'out' => $ out); if (! Empty ($ query) & is_array ($ query) {array_push ($ cmd, array ('query' => $ query);} if (! Empty ($ sort) & is_array ($ sort) {array_push ($ cmd, array ('sort '=> $ query);} if (! Empty ($ limit) & is_int ($ limit) & $ limit> 0) {array_push ($ cmd, array ('limit '=> $ limit ));} if (! Empty ($ keeptemp) & is_bool ($ keeptemp) {array_push ($ cmd, array ('keeptemp '=> $ keeptemp);} if (! Empty ($ finalize) {$ finalize = new coding code ($ finalize); array_push ($ cmd, array ('finalize' => $ finalize);} if (! Empty ($ scope) {array_push ($ cmd, array ('scope '=> $ scope);} if (! Empty ($ jsMode) & is_bool ($ jsMode) {array_push ($ cmd, array ('jsmode' => $ jsMode);} if (! Empty ($ verbose) & is_bool ($ verbose) {array_push ($ cmd, array ('verbose '=> $ verbose ));} $ dbname = $ this-> curr_db_name; $ cmdresult = $ this-> mongo-> $ dbname-> command ($ cmd); if ($ returnresult) {if ($ cmdresult & $ cmdresult ['OK'] = 1) {$ result = $ this-> find ($ out, array ());}} if ($ keeptemp = false) {// delete a collection $ this-> mongo-> $ dbname-> dropCollection ($ out);} return $ result ;}
Official MongoDB website:
MapReduce introduction http://docs.mongodb.org/manual/core/map-reduce/
Aggregation introduction http://docs.mongodb.org/manual/aggregation/
The above introduces the mapreduce usage and php sample code of mongodb, including the content, and hope to be helpful to friends who are interested in PHP tutorials.