2016-04-02 16 views
0

Belgeler arasında mongoDB koleksiyonunda filtrelenen bir işlev var ve bir Cuma, Cumartesi veya Pazar ile eşleşen bir tarihe sahip tüm sonuçları döndürür. Bu beklendiği gibi davranır. Ancak, şimdi bu sonuçları, hafta sonu gelip gelmeyeceklerini belirlemek için eşleştirmem gerekiyor, ancak while döngüsüm yalnızca üç sonuç vermesi gerektiğinde bir sonuç döndürüyor. Neyin yanlış gidiyor?Bu süre döngüde neyin var?

//FIND ALL ENTRIES THAT FALL ON A WEEKEND 
function weekendPlans(callback) { 
    Entry.aggregate(
     [ 
      { "$redact": { 
       "$cond": { 
        "if": { 
         "$or": [ 
          { "$eq": [ { "$dayOfWeek": "$selectedDate" }, 1 ] }, 
          { "$eq": [ { "$dayOfWeek": "$selectedDate" }, 6 ] }, 
          { "$eq": [ { "$dayOfWeek": "$selectedDate" }, 7 ] } 
         ] 
        }, 
        "then": "$$KEEP", 
        "else": "$$PRUNE" 
       } 
      }} 
     ], 
     // GET THE RESULTS AND RETURN IF selectedDate MATCHES THIS WEEKEND 
     function(err,results) { 
     var i = results.length; 
     var theWeekend; 
     console.log(results) 

     // EVERYTHING WORKS UNTIL HERE 
     while(i--) { 
      if(results[i].selectedDate === friday || saturday || sunday) { 
       theWeekend = results[i]; 
       //console.log(theWeekend); 
       break; 
      } 
     } 
     callback(err, theWeekend) 
     } 
)}; 

Beklenen sonuç:

[ { _id: 56fffb6ceb76276c8f39e3f4, 
    url: 'http://wellnessmama.com/13700/benefits-coconut-oil-pets/', 
    title: 'Benefits of Coconut Oil for Pets - Wellness Mama', 
    selectedDate: Sat Apr 02 2016 01:00:00 GMT+0100 (BST), 
    __v: 0 }, 
    { _id: 56fffb8eeb76276c8f39e3f5, 
    url: 'https://news.ycombinator.com/item?id=11404770', 
    title: 'The Trouble with CloudFlare | Hacker News', 
    selectedDate: Sun Apr 03 2016 01:00:00 GMT+0100 (BST), 
    __v: 0 }, 
    { _id: 56fffb5ceb76276c8f39e3f3, 
    url: 'http://londonist.com/2015/11/where-to-eat-and-drink-in-balham', 
    title: 'Where To Eat And Drink In... Balham | Londonist', 
    selectedDate: Fri Apr 01 2016 01:00:00 GMT+0100 (BST), 
    __v: 0 } ] 

Güncel sonucu:

{ _id: 56fffb5ceb76276c8f39e3f3, 
    url: 'http://londonist.com/2015/11/where-to-eat-and-drink-in-balham', 
    title: 'Where To Eat And Drink In... Balham | Londonist', 
    selectedDate: Fri Apr 01 2016 01:00:00 GMT+0100 (BST), 
    __v: 0 } 
+0

Bu "kırılma" mı? – Thomas

cevap

0

Değişim

results[i].selectedDate === friday || saturday || sunday 

için
~results[i].selectedDate.indexOf('Fri') || 
~results[i].selectedDate.indexOf('Sat') || 
~results[i].selectedDate.indexOf('Sun') 

Çünkü her değişkeni bir değerle ve gün dizesi tarih dizesinde ise kontrol etmeniz gerekir.