Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. # pymongo
  2. mapTotalTimeListened = Code("function(){emit(this._id, {totalTimeListened: this.value1.totalTimeListened});}")
  3.  
  4. mapTimeListened = Code("function(){emit(this._id, {radio:this.value2.radio, timeListened: this.value2.timeListened});}")
  5.  
  6. reduceJoin = Code("function(key, values){
  7. var tmps = [];
  8. var results = {};
  9. values.forEach(function(v){
  10. var out = {};
  11. if(v.radio !== undefined )out['radio'] = v.radio;
  12. if(v.timeListened !== undefined ) out['timeListened'] = v.timeListened;
  13. if(v.totalTimeListened !== undefined ) out['totalTimeListened'] = v.totalTimeListened;
  14. if(Object.keys(out).length > 0) tmps.push(out);
  15. });
  16. if(Object.keys(tmps).length > 0) results['fucks'] = tmps;
  17. return results;}")
  18. db.result1.map_reduce(mapTotalTimeListened, reduceJoin, 'joinedTest2',reduce_output=True, merge_output=True)
  19. db.result2.map_reduce(mapTimeListened, reduceJoin,"joinedTest2", reduce_output=True, merge_output = True)
  20.  
  21. // js script
  22. var db = connect('127.0.0.1:27017/local');
  23.  
  24. mapTotalTimeListened = function(){
  25. emit(this._id, {totalTimeListened: this.value1.totalTimeListened});}
  26.  
  27. mapTimeListened = function(){
  28. emit(this._id, {radio:this.value2.radio, timeListened: this.value2.timeListened});}
  29. reduceFine = function(key, values){
  30. var tmps = [];
  31. var results = {};
  32. values.forEach(function(v){
  33. var out = {};
  34. if(v.radio !== undefined )out['radio'] = v.radio;
  35.  
  36. if(v.timeListened !== undefined ) out['timeListened'] = v.timeListened;
  37.  
  38. if(v.totalTimeListened !== undefined ) out['totalTimeListened'] = v.totalTimeListened;
  39. if(Object.keys(out).length > 0) tmps.push(out);
  40. });
  41. if(Object.keys(tmps).length > 0) results['fucks'] = tmps;
  42. return results;
  43. }
  44.  
  45. db.result1.mapReduce(mapTotalTimeListened, reduceFine, {out : {reduce:'joinedRes'}});
  46. db.result2.mapReduce(mapTimeListened, reduceFine, {out : {reduce:'joinedRes'}});
  47.  
  48. inputcollection result1:
  49. {'_id': {'_id': ObjectId('5cb743e78447b053746613b5'),
  50. 'country': 'America',
  51. 'first_name': 'pnklbxzojo',
  52. 'gender': 1,
  53. 'last_name': 'gnzdollddp',
  54. 'vehicule': 'Ford Flex'},
  55. 'value1': {'totalTimeListened': 690238990000.0}}
  56. inputcollection result2:
  57. {'_id': {'_id': ObjectId('5cb743e78447b053746613b5'),
  58. 'country': 'America',
  59. 'first_name': 'pnklbxzojo',
  60. 'gender': 1,
  61. 'last_name': 'gnzdollddp',
  62. 'vehicule': 'Ford Flex'},
  63. 'value2': {'radio': 'Soundtrack', 'timeListened': 4331162000.0}}
  64.  
  65. pymongo output:
  66. {'_id': {'_id': ObjectId('5cb743e78447b053746613b5'),
  67. 'country': 'America',
  68. 'first_name': 'pnklbxzojo',
  69. 'gender': 1,
  70. 'last_name': 'gnzdollddp',
  71. 'vehicule': 'Ford Flex'},
  72. 'value': {'radio': 'Soundtrack', 'timeListened': 4331162000.0}}
  73.  
  74. js script get this output expected:
  75. {"_id":{"country":"America",
  76. "_id":"5cb743e78447b053746613b5",
  77. "gender":1,
  78. "vehicule":"Ford Flex",
  79. "last_name":"gnzdollddp",
  80. "first_name":"pnklbxzojo"},
  81. "value":{"fucks":[{"radio":"Soundtrack","timeListened":4331162000},
  82. {"totalTimeListened":690238990000}]}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement