Guest

Untitled

By: a guest on Jan 28th, 2012  |  syntax: None  |  size: 1.72 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. ## Map/Reduce
  2.  
  3. function(doc) {
  4.   if (doc.type == 'user') {
  5.     emit(doc._id, doc);
  6.   } else if (doc.type == 'picture' && doc.published_datetime > 1) {
  7.     delete doc.exif;
  8.     emit(doc.user_id, doc);
  9.   }
  10. }
  11.  
  12. function(key, values) {
  13.   var dump = [], pics = [];
  14.   for (var x = 0, y = values.length; x < y; x++) {
  15.     var doc = values[x];
  16.     if (doc.type == 'user') {
  17.       dump.push(doc);
  18.     } else if (doc.type == 'picture') {
  19.       pics.push(doc);
  20.     }
  21.   }
  22.   pics.sort(function(a, b) {
  23.     return a.published_datetime < b.published_datetime;
  24.   });
  25.   for (var x = 0; x < 10; x++) {
  26.     var doc = pics.shift();
  27.     if (doc) {
  28.       dump.push(doc);
  29.     }
  30.   }
  31.   return dump;
  32. }
  33.  
  34. ## Results
  35.  
  36. {
  37.   "total_rows": 2,
  38.   "offset": 0,
  39.   "rows": [
  40.     {
  41.       "key": "user1",
  42.       "values": [
  43.         {
  44.           "_id" : "4s5s56absns",
  45.           "type": "user",
  46.           "username": "user1"
  47.         },
  48.         {
  49.           "_id": "53n6shan88sj",
  50.           "type": "picture",
  51.           "user_id": "4s5s56absns",
  52.           "filename": "pic1"
  53.         },
  54.         {
  55.           "_id": "5a7sk100lanms",
  56.           "type": "picture",
  57.           "user_id": "4s5s56absns",
  58.           "filename": "pic2"
  59.         }
  60.       ]
  61.     },
  62.     {
  63.       "key": "user2",
  64.       "values": [
  65.         {
  66.           "_id" : "x56628177200",
  67.           "type": "user",
  68.           "username": "user2"
  69.         },
  70.         {
  71.           "_id": "53n6shan88sj",
  72.           "type": "picture",
  73.           "user_id": "x56628177200",
  74.           "filename": "pic1"
  75.         },
  76.         {
  77.           "_id": "53n6shan88sj",
  78.           "type": "picture",
  79.           "user_id": "x56628177200",
  80.           "filename": "pic1"
  81.         }
  82.       ]
  83.     }
  84.   ]
  85. }