Last active
          November 15, 2016 22:46 
        
      - 
      
 - 
        
Save chunni/1cae5389dd18ee48b277 to your computer and use it in GitHub Desktop.  
Revisions
- 
        
chunni revised this gist
Jul 11, 2014 . 1 changed file with 0 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1 @@ Using cron to perform incremental Map-Reduce in MongoDB  - 
        
chunni revised this gist
Jul 11, 2014 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1 +1,4 @@ Using cron to perform incremental Map-Reduce in MongoDB This is for the blog of: - [Perform incremental data aggregation with MongoDB's MapReduce and cron](http://meetfp.com/en/blog/mongo-mapreduce-cron) - [用MongoDB的增量MapReduce和cron实现增量计数](http://meetfp.com/zh/blog/mongo-mapreduce-cron)  - 
        
chunni revised this gist
Jul 11, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1 +1 @@ Using cron to perform incremental Map-Reduce in MongoDB  - 
        
chunni renamed this gist
Jul 11, 2014 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. - 
        
chunni revised this gist
Jul 11, 2014 . 6 changed files with 1 addition and 0 deletions.There are no files selected for viewing
File renamed without changes.File renamed without changes.File renamed without changes.File renamed without changes.File renamed without changes.This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ Using cron to perform incremental Map-Reduce in MongoDB  - 
        
chunni revised this gist
Jul 11, 2014 . No changes.There are no files selected for viewing
 - 
        
chunni created this gist
Jul 11, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ 01 0 * * * /path/to/inc_tags_job.sh This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,27 @@ //convert ISO data to ObjectId so that it can be compared with ObjectIds in db var ISODateToObjectId = function(date){ // Convert date to hex seconds var hexSeconds = Math.floor(date.getTime()/1000).toString(16); // Create an ObjectId with the hex timestamp var id = ObjectId(hexSeconds + "0000000000000000"); return id } var conn = new Mongo(); var db = conn.getDB("your_db"); var start = new Date(); start.setDate(start.getDate() - 1) // The Map and the Reduce functions have been stored into system.js // and they can be used in the mapReduce function now db.yeedd.mapReduce( mapTags, reduceTags, { query: { _id: { $gt: ISODateToObjectId(start) } }, out: { reduce: "calc_tags" } } ); } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,3 @@ # execute javascript file with mongo shell. # please use your own file path and database path instead /usr/bin/mongo "localhost:27017/db" /path/to/inc_tags.js This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,45 @@ //The Map function, simply count tags var mapTags = function() { if (!this.tags) { return; } for (index in this.tags) { emit(this.tags[index], 1); } }; //The Reduce function, accumulate the count var reduceTags = function(previous, current) { var count = 0; for (index in current) { count += current[index]; } return count; } var conn = new Mongo(); var db = conn.getDB("your_db"); //save the functions db.system.js.save( { _id: "mapTags", value : mapTags } ) db.system.js.save( { _id: "reduceTags", value : reduceTags } ) //execute the Map-Reduce, //the results will be stored in the calc_tags collection db.yeedd.mapReduce( mapTags, reduceTags, { out: "calc_tags" } ) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,3 @@ # execute javascript file with mongo shell. # please use your own file path and database path instead /usr/bin/mongo "localhost:27017/db" /path/to/init_tags.js