Reduce map of MongoDB with query

I have a fairly large MongoDB that I need to retrieve statistics, and I make this purchase by running the "Reduce Map" query.

The problem now is that I need to narrow down the request in order to use, for example, the status: 'drafted' instead of using the entire collection.

This is my Map / Reduce code (I use Codeigniter): I tried the last step in this query, but I can’t get the results, so I think I'm adding the syntax incorrectly: http://cookbook.mongodb.org/patterns/ unique_items_map_reduce / .

$map = new MongoCode ("function() {

                day = Date.UTC(this.created_at.getFullYear(), this.created_at.getMonth(), this.created_at.getDate());

                emit ({day: day, _id: this._id}, {created_at: this.created_at, count: 1});

            }");

            $reduce = new MongoCode ("function( key , values ) {

                var count = 0;

                values.forEach (function(v) {

                    count += v['count'];

                });

                return {count: count};

            }");

            $outer = $this->cimongo->command (array (

                "mapreduce" => "documents",   

                "map"       => $map,   

                "reduce"    => $reduce,  

                "out"       => "stats_results"

            ));


            $map = new MongoCode ("function() {

                emit(this['_id']['day'], {count: 1});

            }");

            $reduce = new MongoCode ("function( key , values ) {

                var count = 0;

                values.forEach (function(v) {

                    count += v['count'];

                });

                return {count: count};

            }");

            $outer = $this->cimongo->command (array (

                "mapreduce" => "stats_results",   

                "map"       => $map,   

                "reduce"    => $reduce,   

                "out"       => "stats_results_unique"

            ));
+5
source share
2 answers

Two things about your question:

1) , . :

, :

{
    "url" : "http://example.com/page8580.html",
    "user_id" : "Jonathan.Clark",
    "date" : ISODate("2012-06-11T10:59:36.271Z")
}

JavaScript map/reduce, URL.

// Map function:

map = function() {
  emit({ url: this.url }, {count: 1});
}

// Reduce function:

reduce = function(key, values) {
    var count = 0;

    values.forEach(
    function(val) { count += val['count']; }
    );

    return {count: count};
};

// Run the Map/Reduce function across the 'pageviews' collection:
// Note that MongoDB will store the results in the 'pages_per_day'
//   collection because the 'out' parameter is present

 db.pageviews.mapReduce( 
    map,        // pass in the 'map' function as an argument
    reduce,     // pass in the 'reduce' function as an argument
    // options
    { out: 'pages_per_day',     // output collection
      verbose: true }       // report extra statistics
);

2) Map/Reduce " ", "mapReduce()", , 'map()' :

// Run the Map/Reduce function across the 'pageviews' collection, but 
// only report on the pages seen by "Jonathan.Clark"

 db.pageviews.mapReduce( 
    map,        // Use the same map & reduce functions as before
    reduce,     
    { out: 'pages_per_day_1user',       // output to different collection
      query:{ 'user_id': "Jonathan.Clark" }     // query descriptor
      verbose: true }       
);

: JavaScript, , .

3) Map/Reduce PHP:

$outer = $this->cimongo->command (array (
                "mapreduce" => "pageviews",   
                "map"       => $map,   
                "reduce"    => $reduce,   
                "out"       => "pages_per_day_1user",
                "query"     => array( "user_id" => "Jonathan.Clark" )
            ));

4) Map/Reduce . :

+12

rock mongo mongo3 / .

rock_mongo, .

+1

All Articles