2012-06-10 5 views
5

私は統計情報を抽出する必要があるむしろ大きなMongoDBを取得しました。私はMap Reduceクエリを実行して購入します。MongoDBマップでクエリを減らす

問題は今、私は例の状態で使用するクエリ狭くする必要があるということです。代わりに、コレクション全体を使っての「」起草を

これは私の地図/コードを(私はCodeIgniterのを使用しています)の削減です。 私は、このクエリの最後のステップに従うことを試みたが、私は文法が間違っ追加だと思うので、私は結果を得ることができません:。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

あなたは " '起草' 使用状況" とは何を意味するのですか?テーブル全体をマッピングしないようにしたいのですか、それともそのステータスのキーだけを放ちますか?あなたが望むのは、状況フィールドに基づいて条件付きで結果を出すことだと思います。 – evnu

答えて

1

あなたは減らす/マップに関する詳細な統計情報と結果を得るためにrock mongoまたはmongo3を使用することができますかインデックス。

私はprefer rock_mongoは、より多くの機能があります。あなたの質問について

12

2つのこと:

1)料理の例は、あなたが何をしようとしてのビットが複雑すぎるかもしれません。ここでは1簡単です:次のようになります文書構造を考えると

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

ここでのJavaScriptコードの個別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)あなたは「ページビュー」コレクションのサブセットのみに機能を削減/地図を実行する場合は、数を制限するために、「MapReduceの()」の呼び出しにクエリを指定することができますあなたはJavaScriptを使用していない場合は、あなたが使用しているどんなプログラミング言語にこれらの呼び出しを翻訳する必要がありますことを

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

注:「マップ()」関数は、上で動作します文書の。

+0

クエリのparamは私が探しているものですが、値の配列を照会することは可能ですか?たとえば、find()の中では_id:{$ in:[1,2,3,4]}のようなものを使用します。 – abritez

+0

'query'パラメータには、任意のMongoDBクエリを記述するドキュメントが含まれています。そのフィールドで好きな構文を使用できます。 –

関連する問題