Guest User

Untitled

a guest
Jul 15th, 2012
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. Mongo DB MapReduce: Emit key from array based on condition
  2. {
  3. "_id" : ObjectId("4fc5ed3e67960de6794dd21c"),
  4. "name" : "some name",
  5. "uid" : "some app specific uid",
  6. "collection" : "some name",
  7. "metadata" : [
  8. {
  9. "key" : "key1",
  10. "value" : "Plain text",
  11. "status" : "SINGLE_RESULT",
  12. },
  13. {
  14. "key" : "key2",
  15. "value" : "text/plain",
  16. "status" : "SINGLE_RESULT",
  17. },
  18. {
  19. "key" : "key3",
  20. "value" : 3469,
  21. "status" : "OK",
  22. }
  23. ]
  24. }
  25.  
  26. function map() {
  27. var mime = "";
  28. this.metadata.forEach(function (m) {
  29. if (m.key === "key2") {
  30. mime = m.value;}
  31. });
  32. emit(mime, {count:1});
  33. }
  34.  
  35. function reduce() {
  36. var res = {count:0};
  37. values.forEach(function (v) {res.count += v.count;});
  38. return res;
  39. }
  40.  
  41. db.collection.mapReduce(map, reduce, {out: { inline : 1}})
  42.  
  43. {
  44. "_id" : ObjectId("4fc5ed3e67960de6794dd21c"),
  45. "name" : "some name",
  46. "uid" : "some app specific uid",
  47. "collection" : "some name",
  48. "metadata" : {
  49. "key1" : {
  50. "value" : "Plain text",
  51. "status" : "SINGLE_RESULT"
  52. },
  53. "key2": {
  54. "value" : "text/plain",
  55. "status" : "SINGLE_RESULT"
  56. },
  57. "key3" : {
  58. "value" : 3469,
  59. "status" : "OK"
  60. }
  61. }
  62. }
  63.  
  64. function map() {
  65. emit( this.metadata["key2"].value, { count : 1 } );
  66. }
  67.  
  68. function map() {
  69. var mime = "";
  70. this.metadata.forEach(function (m) {
  71. if (m.key === "key2") {
  72. mime = m.value;
  73. break;
  74. }
  75. });
  76. emit(mime, {count:1});
  77. }
Advertisement
Add Comment
Please, Sign In to add comment