2016-04-02 137 views
0

我有一個函數,可以通過mongoDB集合中的文檔進行過濾,並返回所有匹配星期五,星期六或星期日的結果。這表現如預期。但是,現在我需要匹配這些結果以確定它們是否會在週末到來,但是,while循環只返回三個結果。出了什麼問題?這個while循環有什麼問題?

//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) 
     } 
)}; 

預期結果:

[ { _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 } ] 

當前的結果:

{ _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

這是'break'嗎? – Thomas

回答

0

變化

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

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

因爲您需要檢查每個變量的值,如果日期字符串在日期字符串中。