2016-03-03 55 views
0

我想創建一個不參加教師課程的學生列表。在Meteor中過濾發佈內容

此列表旨在向所有未註冊課程的學生展示教師。

  • 老師帳戶包含他們教授的課程。
  • 學生帳戶包含他們將要參加的課程。

我不確定是否可以檢查他們教授的課程的教師(當前用戶)帳戶,然後在發佈數據時過濾掉所有已選擇該課程的學生。我試過了一些變化,但沒有任何工作。

路徑:schemas.js

Schema.classes = new SimpleSchema({ 
     class: { 
     type: [String], 
     optional: true, 
     autoform: { 
      type: "select-checkbox", 
      options: function() { 
       return [ 
       {label: "Maths", value: 'Maths' }, 
       {label: "English", value: 'English'}, 
       {label: "Science", value: 'Science'}, 
       ]; 
      }, 
      afFormGroup: { 
       label: false 
      } 
     } 
    } 
}); 

路徑:publish.js

Meteor.publish('classes', function() { 
    return Meteor.users.find({roles:'is_student'}); 
}); 
+0

可能重複[流星 - 在發佈中創建一個變量](http://stackoverflow.com/questions/35784424/meteor-creating-a-variable-within-publish) –

回答

0

假設你的文檔結構如下:

Users.insert({ 
    firstName: "Matthias", 
    lastName: "Eckhart", 
    isTeacher: true, 
    classes: ["Secure Coding", "Hacking", "Software Design", "Software Quality", "Entrepreneurship"], 
    office: "C221" 
}); 
Users.insert({ 
    firstName: "John", 
    lastName: "Doe", 
    isTeacher: false, 
    classes: ["Hacking", "Entrepreneurship", "Secure Coding"], 
}); 
Users.insert({ 
    firstName: "Jane", 
    lastName: "Doe", 
    isTeacher: false, 
    classes: ["Web Design", "Database Design", "Hacking"], 
}); 
Users.insert({ 
    firstName: "Richard", 
    lastName: "Roe", 
    isTeacher: false, 
    classes: ["English", "German"], 
}); 

然後,你可以得到一切如果你想要得到誰不參加通過一定的老師教所有班學生

Users.find({ 
    isTeacher: false, 
    classes: { 
    $nin: ["Hacking"] // Add the classes you want to filter out here 
    } 
}); 

:沒有誰做了一定的當前類的classes陣列使用$nin MongoDB中比較查詢運算符的學生,你可以結合$not$all操作:

Users.find({ 
    isTeacher: false, 
    classes: { 
    $not: { 
     $all: ["Secure Coding", "Hacking", "Software Design", "Software Quality", "Entrepreneurship"] 
    } 
    } 
}); 
+0

感謝您嘗試Matthias。我不認爲我解釋了我所要求的內容。我今天早上轉貼了減少的代碼片斷。隨時檢查一下。 http://stackoverflow.com/questions/35784424/meteor-creating-a-variable-within-publish?noredirect=1#comment59240768_35784424 – bp123