我想创建一个没有参加教师课程的学生名单。
此列表旨在向所有未注册课程的学生展示教师。
我不确定是否可以检查他们教授的课程的教师(当前用户)帐户,然后筛选出发布数据时已经选择该课程的所有学生。我尝试了很多变化,但没有任何效果。
路径:new-device-connect
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
答案 0 :(得分: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"],
});
然后,您可以使用 $nin
MongoDB比较查询运算符,让所有未在classes
数组中出现某个班级的学生:
Users.find({
isTeacher: false,
classes: {
$nin: ["Hacking"] // Add the classes you want to filter out here
}
});
如果您希望学生不参加由某位教师授课的所有课程,您可以将 $not
与 {结合起来{3}} 运营商:
Users.find({
isTeacher: false,
classes: {
$not: {
$all: ["Secure Coding", "Hacking", "Software Design", "Software Quality", "Entrepreneurship"]
}
}
});