2012-06-10 21 views
5

Ho ottenuto un MongoDB piuttosto grande di cui ho bisogno per estrarre statistiche e compro questo acquisto eseguendo una query Riduci mappa.Riduci la mappa MongoDB con la query

Il problema ora è che ho bisogno di restringere la query per utilizzare ad esempio lo stato:. 'Redatto" invece di utilizzare l'intera collezione

Questo è il mio Map/Reduce codice (sto usando Codeigniter): ho cercato di seguire l'ultimo passo in questa interrogazione, ma non riesco a ottenere risultati in modo penso che aggiungo il torto sintassi:. 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" 

      )); 
+0

Che cosa si intende con "status uso 'redatto'"? Vuoi evitare di mappare l'intera tabella, o è sufficiente emettendo solo le chiavi con quello stato? Immagino che quello che vuoi sia emettere condizionatamente il risultato in base al campo dello stato. – evnu

risposta

1

È possibile utilizzare rock mongo o mongo3 per ottenere statistiche dettagliate e risultati sulla mappa/ridurre o indici

I preffer rock_mongo, ha più funzioni.

12

Due cose sulla tua domanda:

1) L'esempio nel libro di cucina potrebbe essere un po 'troppo complesso per quello che stai cercando di fare. Ecco uno più semplice:

Data una struttura del documento che assomiglia a questo:

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

Ecco qualche esempio di codice JavaScript per eseguire una mappa/ridurre lavoro che conterà il numero di visite per URL distinti.

// 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) Se si desidera eseguire la mappa/funzione di Riduzione solo su un sottoinsieme della collezione 'pagine visualizzate', è possibile specificare una query per la chiamata a 'MapReduce()', al fine di limitare il numero di di documenti che la funzione 'map()' opererà su:

// 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 }  
); 

si noti che se non si utilizza JavaScript, dovrete tradurre queste chiamate in qualunque linguaggio di programmazione che si sta utilizzando.

3) Ecco un esempio di chiamata mappa/funzione di Riduzione con una condizione di query utilizzando PHP:

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

4) Per ulteriori informazioni su Map/Reduce, vedere i seguenti riferimenti:

+0

La query param è ciò che sto cercando, tuttavia è possibile interrogare una matrice di valori? per esempio in find() uso qualcosa come _id: {$ in: [1,2,3,4]} – abritez

+0

Il parametro 'query' contiene un documento che descrive una query arbitraria di MongoDB; puoi usare qualsiasi sintassi valida che ti piace in quel campo. –

Problemi correlati