我在Mongo有一个名为Programs的集合,其中一个文档看起来像这样(我在Meteor中):
{ id: "gMyxyez43sxya",
.
.
Teachers: {
Week1: { Sunday: "SAM", Monday: "GEORGE" },
Week2: { Sunday: "FIONA", Monday: "JEFFERS" }
},
CampYear: "ssipc16",
}
我需要"Week1", "Week2"
属性作为变量。变量在这里计算:
var week = "Week" + Session.get('CurrentWeek').substr(0, 1); //this would render 'Week1', for example
var teacher = document.getElementById('sunday').value;
我需要的查询是:
Programs.find({ "Teachers.week.Sunday": teachers });
但这不起作用。所以我构建了这样的对象:
var query = { Teachers: {} };
query.Teachers[week].Sunday = teacher;
然后:
Programs.find(query).count();
但我收到类型错误'Uncaught TypeError: Cannot set property 'Sunday' of undefined'
。
我也尝试过这样:
query.Teachers[week] = { 'Sunday': teacher };
Programs.find(query).count();
没有错误但每次都在计算0的计数。
如何使这项工作?
答案 0 :(得分:2)
我找到了解决方案:我需要像这样构建它:
var teacher = document.getElementById('sunday-lessons-students').value;
var week = "Week" + Session.get('CurrentWeek').substr(0, 1);
var query = {};
query['Teachers.' + week + '.Sunday'] = teacher;
console.log(Programs.find(query).count());