JavaScript / mongoose - 循环遍历数组以创建对象数组

时间:2013-04-01 00:00:34

标签: javascript node.js mongoose

目前我有一个团队ID列表,我已将其存储在数组teamIds中。我的findByKey mongoose模式中还有一个函数Team,它根据其键找到一个团队。从代码中可以看出,我必须将每个新的Team.findByKey放在前一个Team.findByKey中,如果我将它放在前一个findByKey之外,它就无法正常工作。

我在将teamIds方法放入某种for循环时遇到问题,这种循环可以遍历数组Team和数组中的每个teamsList。我正在开发创建新锦标赛的功能,想法是你将一个teamIds数组传递到这里,然后循环遍历每个ID,搜索该团队并将其添加到Team数组中以便创建比赛。

我需要将最后一段代码放到最后Team.findByKey方法中创建n个对象的数组,这是我的问题。我如何解决这个问题,以便我可以通过Teams个团队,找到每个团队并将其存储为 app.get('/tournament', function(req, res){ function genMatches(nTeams) { var matchArray = []; while (nTeams > 1) { nTeams = nTeams >> 1; var matches = []; for (var i = 0; i < nTeams; ++i) { matches.push([]); } matchArray.push(matches); } return matchArray; } var teamIds = [ 1364472344972, 1363173222886, 1363007586845, 1363007557484 ] var tournamentName = 'My Tournament'; Team.findByKey(teamIds[0], function(err, team1) { if(err) { util.log("Error occured"); } if(!team1) { util.log("The team does not exist"); } Team.findByKey(teamIds[1], function(err, team2) { if(err) { return util.log("Error occured"); } if(!team2) { return util.log("The team does not exist"); } Team.findByKey(teamIds[2], function(err, team3) { if(err) { return util.log("Error occured"); } if(!team3) { return util.log("The team does not exist"); } Team.findByKey(teamIds[3], function(err, team4) { if(err) { return util.log("Error occured"); } if(!team4) { return util.log("The team does not exist"); } var teamList = [ team1._id, team2._id, team3._id, team4._id ]; var numRounds = Math.log(teamList.length)/Math.log(2); var matches = genMatches(teamList.length); for(var i = 0; i < matches[0].length; i++){ matches[0][i] = [ teamList[i+i], teamList[i+(i+1)] ] } var tournament = new Tournament({ name: tournamentName, teams: teamList, rounds: numRounds, matches: matches }); res.send(tournament); }); }); }); }); }); 数组?

这是我的代码:

'use strict';

var util = require('util');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var validatePresenceOf = function(value){
  return value && value.length; 
};

var toLower = function(string){
  return string.toLowerCase();
};

var getId = function(){
  return new Date().getTime();
};

/**
  * The Team schema. we will use timestamp as the unique key for each team
  */
var Team = new Schema({
  'key' : {
    unique : true,
    type : Number,
    default: getId
  },
  'name' : { type : String,
              validate : [validatePresenceOf, 'Team name is required'],
              index : { unique : true }
            }
});

/**
  * Get complete team details for all the teams
  */
Team.statics.getAll = function(cb){
  var query = this.find({});
  query.sort({key : -1});
  return query.exec(cb);
};

Team.statics.findByKey = function(key, cb){
  return this.find({'key' : key}, cb);
};

Team.statics.findByName = function(name, cb) {
  return this.find({'name' : name}, cb);
};

module.exports = mongoose.model('Team', Team);

团队架构:

{{1}}

1 个答案:

答案 0 :(得分:1)

您可以使用$in运算符在一个查询中查找您的四个小组:

Team.find({key: {$in: teamIds}}, function (err, teams) {
  ...
});

如果出于某种原因需要多次致电findByKey而不是将其全部合并到一个$in,请查看使用async库的each方法进行操作那更干净。