Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Meteor 如何计算集合中相似ID的数量?_Meteor - Fatal编程技术网

Meteor 如何计算集合中相似ID的数量?

Meteor 如何计算集合中相似ID的数量?,meteor,Meteor,我有一个用种子填充的集合,看起来像这样 if (Schools.find().count() === 0) { school1Id = Schools.insert({ name: "High School For Boys", housename: { house1: 'Milton', house2: 'Granton', house3: 'Buxton', house4: 'Barnard' } })

我有一个用种子填充的集合,看起来像这样

if (Schools.find().count() === 0) {
school1Id = Schools.insert({
    name: "High School For Boys",
    housename: {
        house1: 'Milton',
        house2: 'Granton',
        house3: 'Buxton',
        house4: 'Barnard' 
    }
});
school2Id = Schools.insert({
    name: "Pretoria Boys High",
    housename: {
        house1: 'House1',
        house2: 'House2',
        house3: 'House3',
        house4: 'House4' 
    }
});
if (Years.find().count() === 0) {
for (y = 1905; y < 2015; y++ ) {
    Years.insert({
        year: y
    });
}
}
if (Meteor.users.find().count() === 0) {
  var school1Id = Schools.findOne({name: "High School For Boys"})._id;
  var school2Id = Schools.findOne({name: "Pretoria Boys High"})._id;

  seed1UserId = Accounts.createUser({
    username: 'lbob',
    email: 'l@oo.com',
    password: '123456',
    profile: {
        firstname: 'Lance',
        lastname: 'Bones',
            phone: '0000000000',
        schoolName: school1Id,
        firstschoolyear: Years.findOne({year: 1984})._id,
        lastschoolyear: Years.findOne({year: 1988})._id,
        matriculated: 1,
        housename: Schools.findOne(school1Id).housename.house1,
        country: 'South Africa',
        cityofresidence: 'Cape Town',
        emplindustry: 'IT',
        joined: new Date() // current time
      }
  });
  seed2UserId = Accounts.createUser({
    username: 'david',
    email: 'd@oo.com',
    password: '123456',
    profile: {
    firstname: 'David',
    lastname: 'McNulty',
    phone: '0000000000',
    schoolName: school1Id,
    firstschoolyear: Years.findOne({year: 1988})._id,
    lastschoolyear: Years.findOne({year: 1990})._id,
    matriculated: 0,
    housename: Schools.findOne(school1Id).housename.house1,
    country: 'Scotlad',
    cityofresidence: 'Glasgow',
    emplindustry: 'Hospitality',
    joined: new Date() // current time
  }
  });
  seed3UserId = Accounts.createUser({
    username: 'glenn',
    email: 'g@oo.com',
    password: '123456',
    profile: {
        firstname: 'Glenn-Douglas',
        lastname: 'Jones',
        phone: '0000000000',
        schoolName: school1Id,
        firstschoolyear: Years.findOne({year: 1981})._id,
        lastschoolyear: Years.findOne({year: 1986})._id,
        matriculated: 1,
        housename: Schools.findOne(school1Id).housename.hous1,
        country: 'South Africa',
        cityofresidence: 'Cape Town',
        emplindustry: 'Coaching',
        joined: new Date() // current time
      }
  });
  seed4UserId = Accounts.createUser({
    username: 'martin',
    email: 'm@oo.com',
    password: '123456',
    profile: {
        firstname: 'Martin',
        lastname: 'James',
        phone: '0000000000',
        schoolName: school2Id,
        firstschoolyear: Years.findOne({year: 1983})._id,
        lastschoolyear: Years.findOne({year: 1987})._id,
        matriculated: 1,
        housename: Schools.findOne(school2Id).housename.house2,
        country: 'Austrailia',
        cityofresidence: 'Melbourne',
        emplindustry: 'Airline',
        joined: new Date() // current time
      }
  });
}
<template name="schoolList">
 <div class="title">Schools List</div>
 <div class="list-group">
    {{#each schools}}
        {{> school}}
    {{/each}}
</div>
<template>

<template name="school">
  <a href="#" class="list-group-item school">
    <!-- <span class="badge">44</span> -->
    <span class="badge">{{totalalumnis}}</span>
    {{name}}
 </a>
<template>
Template.schoolList.helpers({
schools: function () {
    return Schools.find({});
},
totalalumnis: function () {
    // return Schools.find({}'_id');
    //return Meteor.users.findOne()
    return Schools.find({}).count();
}
});
Template.schoolList.helpers({
schools: function () {
    return Schools.find({});
},
totalalumnis: function () {
    return AlumniList.find({schoolName: this._id}).count();
}
});
我有另一个收藏,是这样的种子

if (Schools.find().count() === 0) {
school1Id = Schools.insert({
    name: "High School For Boys",
    housename: {
        house1: 'Milton',
        house2: 'Granton',
        house3: 'Buxton',
        house4: 'Barnard' 
    }
});
school2Id = Schools.insert({
    name: "Pretoria Boys High",
    housename: {
        house1: 'House1',
        house2: 'House2',
        house3: 'House3',
        house4: 'House4' 
    }
});
if (Years.find().count() === 0) {
for (y = 1905; y < 2015; y++ ) {
    Years.insert({
        year: y
    });
}
}
if (Meteor.users.find().count() === 0) {
  var school1Id = Schools.findOne({name: "High School For Boys"})._id;
  var school2Id = Schools.findOne({name: "Pretoria Boys High"})._id;

  seed1UserId = Accounts.createUser({
    username: 'lbob',
    email: 'l@oo.com',
    password: '123456',
    profile: {
        firstname: 'Lance',
        lastname: 'Bones',
            phone: '0000000000',
        schoolName: school1Id,
        firstschoolyear: Years.findOne({year: 1984})._id,
        lastschoolyear: Years.findOne({year: 1988})._id,
        matriculated: 1,
        housename: Schools.findOne(school1Id).housename.house1,
        country: 'South Africa',
        cityofresidence: 'Cape Town',
        emplindustry: 'IT',
        joined: new Date() // current time
      }
  });
  seed2UserId = Accounts.createUser({
    username: 'david',
    email: 'd@oo.com',
    password: '123456',
    profile: {
    firstname: 'David',
    lastname: 'McNulty',
    phone: '0000000000',
    schoolName: school1Id,
    firstschoolyear: Years.findOne({year: 1988})._id,
    lastschoolyear: Years.findOne({year: 1990})._id,
    matriculated: 0,
    housename: Schools.findOne(school1Id).housename.house1,
    country: 'Scotlad',
    cityofresidence: 'Glasgow',
    emplindustry: 'Hospitality',
    joined: new Date() // current time
  }
  });
  seed3UserId = Accounts.createUser({
    username: 'glenn',
    email: 'g@oo.com',
    password: '123456',
    profile: {
        firstname: 'Glenn-Douglas',
        lastname: 'Jones',
        phone: '0000000000',
        schoolName: school1Id,
        firstschoolyear: Years.findOne({year: 1981})._id,
        lastschoolyear: Years.findOne({year: 1986})._id,
        matriculated: 1,
        housename: Schools.findOne(school1Id).housename.hous1,
        country: 'South Africa',
        cityofresidence: 'Cape Town',
        emplindustry: 'Coaching',
        joined: new Date() // current time
      }
  });
  seed4UserId = Accounts.createUser({
    username: 'martin',
    email: 'm@oo.com',
    password: '123456',
    profile: {
        firstname: 'Martin',
        lastname: 'James',
        phone: '0000000000',
        schoolName: school2Id,
        firstschoolyear: Years.findOne({year: 1983})._id,
        lastschoolyear: Years.findOne({year: 1987})._id,
        matriculated: 1,
        housename: Schools.findOne(school2Id).housename.house2,
        country: 'Austrailia',
        cityofresidence: 'Melbourne',
        emplindustry: 'Airline',
        joined: new Date() // current time
      }
  });
}
<template name="schoolList">
 <div class="title">Schools List</div>
 <div class="list-group">
    {{#each schools}}
        {{> school}}
    {{/each}}
</div>
<template>

<template name="school">
  <a href="#" class="list-group-item school">
    <!-- <span class="badge">44</span> -->
    <span class="badge">{{totalalumnis}}</span>
    {{name}}
 </a>
<template>
Template.schoolList.helpers({
schools: function () {
    return Schools.find({});
},
totalalumnis: function () {
    // return Schools.find({}'_id');
    //return Meteor.users.findOne()
    return Schools.find({}).count();
}
});
Template.schoolList.helpers({
schools: function () {
    return Schools.find({});
},
totalalumnis: function () {
    return AlumniList.find({schoolName: this._id}).count();
}
});
学校模板如下所示

if (Schools.find().count() === 0) {
school1Id = Schools.insert({
    name: "High School For Boys",
    housename: {
        house1: 'Milton',
        house2: 'Granton',
        house3: 'Buxton',
        house4: 'Barnard' 
    }
});
school2Id = Schools.insert({
    name: "Pretoria Boys High",
    housename: {
        house1: 'House1',
        house2: 'House2',
        house3: 'House3',
        house4: 'House4' 
    }
});
if (Years.find().count() === 0) {
for (y = 1905; y < 2015; y++ ) {
    Years.insert({
        year: y
    });
}
}
if (Meteor.users.find().count() === 0) {
  var school1Id = Schools.findOne({name: "High School For Boys"})._id;
  var school2Id = Schools.findOne({name: "Pretoria Boys High"})._id;

  seed1UserId = Accounts.createUser({
    username: 'lbob',
    email: 'l@oo.com',
    password: '123456',
    profile: {
        firstname: 'Lance',
        lastname: 'Bones',
            phone: '0000000000',
        schoolName: school1Id,
        firstschoolyear: Years.findOne({year: 1984})._id,
        lastschoolyear: Years.findOne({year: 1988})._id,
        matriculated: 1,
        housename: Schools.findOne(school1Id).housename.house1,
        country: 'South Africa',
        cityofresidence: 'Cape Town',
        emplindustry: 'IT',
        joined: new Date() // current time
      }
  });
  seed2UserId = Accounts.createUser({
    username: 'david',
    email: 'd@oo.com',
    password: '123456',
    profile: {
    firstname: 'David',
    lastname: 'McNulty',
    phone: '0000000000',
    schoolName: school1Id,
    firstschoolyear: Years.findOne({year: 1988})._id,
    lastschoolyear: Years.findOne({year: 1990})._id,
    matriculated: 0,
    housename: Schools.findOne(school1Id).housename.house1,
    country: 'Scotlad',
    cityofresidence: 'Glasgow',
    emplindustry: 'Hospitality',
    joined: new Date() // current time
  }
  });
  seed3UserId = Accounts.createUser({
    username: 'glenn',
    email: 'g@oo.com',
    password: '123456',
    profile: {
        firstname: 'Glenn-Douglas',
        lastname: 'Jones',
        phone: '0000000000',
        schoolName: school1Id,
        firstschoolyear: Years.findOne({year: 1981})._id,
        lastschoolyear: Years.findOne({year: 1986})._id,
        matriculated: 1,
        housename: Schools.findOne(school1Id).housename.hous1,
        country: 'South Africa',
        cityofresidence: 'Cape Town',
        emplindustry: 'Coaching',
        joined: new Date() // current time
      }
  });
  seed4UserId = Accounts.createUser({
    username: 'martin',
    email: 'm@oo.com',
    password: '123456',
    profile: {
        firstname: 'Martin',
        lastname: 'James',
        phone: '0000000000',
        schoolName: school2Id,
        firstschoolyear: Years.findOne({year: 1983})._id,
        lastschoolyear: Years.findOne({year: 1987})._id,
        matriculated: 1,
        housename: Schools.findOne(school2Id).housename.house2,
        country: 'Austrailia',
        cityofresidence: 'Melbourne',
        emplindustry: 'Airline',
        joined: new Date() // current time
      }
  });
}
<template name="schoolList">
 <div class="title">Schools List</div>
 <div class="list-group">
    {{#each schools}}
        {{> school}}
    {{/each}}
</div>
<template>

<template name="school">
  <a href="#" class="list-group-item school">
    <!-- <span class="badge">44</span> -->
    <span class="badge">{{totalalumnis}}</span>
    {{name}}
 </a>
<template>
Template.schoolList.helpers({
schools: function () {
    return Schools.find({});
},
totalalumnis: function () {
    // return Schools.find({}'_id');
    //return Meteor.users.findOne()
    return Schools.find({}).count();
}
});
Template.schoolList.helpers({
schools: function () {
    return Schools.find({});
},
totalalumnis: function () {
    return AlumniList.find({schoolName: this._id}).count();
}
});
我试图统计每所学校的学生人数,并将其显示在徽章中

正如您从js文件中看到的,我正在努力理解如何设置查询是最好的方法。 我不确定我是否创建了错误的数据,我宁愿将学生存储在学校集合中。我的逻辑告诉我,这将更容易计数,并且更具反应性,但是我决定将学校id存储在学生中,逻辑是关于学生的所有信息都在他们的用户文档中

我还想在另一个页面上列出学校每年的学生人数,重复这种类型的搜索,以便我可以将结果添加到年度徽章中

我真的很感激你的帮助

编辑:我对助手代码做了如下更改

if (Schools.find().count() === 0) {
school1Id = Schools.insert({
    name: "High School For Boys",
    housename: {
        house1: 'Milton',
        house2: 'Granton',
        house3: 'Buxton',
        house4: 'Barnard' 
    }
});
school2Id = Schools.insert({
    name: "Pretoria Boys High",
    housename: {
        house1: 'House1',
        house2: 'House2',
        house3: 'House3',
        house4: 'House4' 
    }
});
if (Years.find().count() === 0) {
for (y = 1905; y < 2015; y++ ) {
    Years.insert({
        year: y
    });
}
}
if (Meteor.users.find().count() === 0) {
  var school1Id = Schools.findOne({name: "High School For Boys"})._id;
  var school2Id = Schools.findOne({name: "Pretoria Boys High"})._id;

  seed1UserId = Accounts.createUser({
    username: 'lbob',
    email: 'l@oo.com',
    password: '123456',
    profile: {
        firstname: 'Lance',
        lastname: 'Bones',
            phone: '0000000000',
        schoolName: school1Id,
        firstschoolyear: Years.findOne({year: 1984})._id,
        lastschoolyear: Years.findOne({year: 1988})._id,
        matriculated: 1,
        housename: Schools.findOne(school1Id).housename.house1,
        country: 'South Africa',
        cityofresidence: 'Cape Town',
        emplindustry: 'IT',
        joined: new Date() // current time
      }
  });
  seed2UserId = Accounts.createUser({
    username: 'david',
    email: 'd@oo.com',
    password: '123456',
    profile: {
    firstname: 'David',
    lastname: 'McNulty',
    phone: '0000000000',
    schoolName: school1Id,
    firstschoolyear: Years.findOne({year: 1988})._id,
    lastschoolyear: Years.findOne({year: 1990})._id,
    matriculated: 0,
    housename: Schools.findOne(school1Id).housename.house1,
    country: 'Scotlad',
    cityofresidence: 'Glasgow',
    emplindustry: 'Hospitality',
    joined: new Date() // current time
  }
  });
  seed3UserId = Accounts.createUser({
    username: 'glenn',
    email: 'g@oo.com',
    password: '123456',
    profile: {
        firstname: 'Glenn-Douglas',
        lastname: 'Jones',
        phone: '0000000000',
        schoolName: school1Id,
        firstschoolyear: Years.findOne({year: 1981})._id,
        lastschoolyear: Years.findOne({year: 1986})._id,
        matriculated: 1,
        housename: Schools.findOne(school1Id).housename.hous1,
        country: 'South Africa',
        cityofresidence: 'Cape Town',
        emplindustry: 'Coaching',
        joined: new Date() // current time
      }
  });
  seed4UserId = Accounts.createUser({
    username: 'martin',
    email: 'm@oo.com',
    password: '123456',
    profile: {
        firstname: 'Martin',
        lastname: 'James',
        phone: '0000000000',
        schoolName: school2Id,
        firstschoolyear: Years.findOne({year: 1983})._id,
        lastschoolyear: Years.findOne({year: 1987})._id,
        matriculated: 1,
        housename: Schools.findOne(school2Id).housename.house2,
        country: 'Austrailia',
        cityofresidence: 'Melbourne',
        emplindustry: 'Airline',
        joined: new Date() // current time
      }
  });
}
<template name="schoolList">
 <div class="title">Schools List</div>
 <div class="list-group">
    {{#each schools}}
        {{> school}}
    {{/each}}
</div>
<template>

<template name="school">
  <a href="#" class="list-group-item school">
    <!-- <span class="badge">44</span> -->
    <span class="badge">{{totalalumnis}}</span>
    {{name}}
 </a>
<template>
Template.schoolList.helpers({
schools: function () {
    return Schools.find({});
},
totalalumnis: function () {
    // return Schools.find({}'_id');
    //return Meteor.users.findOne()
    return Schools.find({}).count();
}
});
Template.schoolList.helpers({
schools: function () {
    return Schools.find({});
},
totalalumnis: function () {
    return AlumniList.find({schoolName: this._id}).count();
}
});
我已经在浏览器控制台中测试了查询,如果我输入了有效的学名id,它将返回正确的学生人数。但是,这不会显示在模板中


有人能帮忙吗?

您正在寻找相当于
select count
的NoSQL,对吗?查看MongoDB的
aggregate
函数。需要一个包裹谢谢你的建议。我来看看这个。