I have this mongo collection opponents
:
{username:foo, foo:{}} //the foo embedded document is to store foo's opponents'names and round numbers
{username:foo1,foo1:{}}//the foo1 embedded document is to store foo1's opponents'names and round numbers
{username:foo2,foo2:{}}//the foo1 embedded document is to store foo2's opponents'names and round numbers
And this table
object in the node.js server side:
table = {players:[name:foo,rounds:3],[name:foo1,rounds:3],[name:foo2,rounds:3]} //this is all the players' name in a table with their rounds number
For each player in a table, I want to append their opponents' names and rounds times to their embedded document in the opponent
collection accordingly(of course no need to append themselves). So the desired results is:
{username:foo, foo:{foo1:3,foo2:3}}
{username:foo1,foo1:{foo:3,foo2:3}}
{username:foo2,foo2:{foo:3,foo1:3}}
This is my code:
First I have a custom function getArr(player)
to return each player an array of their opponents names. so getArr(table.players[0])
would return [foo1,foo2]
// loop through the players
for (var i = 0 ;i < table.players.length; i++){
// loop through the getArr() array which is one number shorter than players
for (var j = 0; j< table.players.length -1; j++){
console.log(i) // print "i" 6 times?
db.opponents.update({username:table.players[i].name},{$inc:{[[table.players[i].name][getArr(table.players[i])[j]]]:table.players[i].rounds}})
}
}
Issue one: [[table.players[i].name][getArr(table.players[i])[j]]]
is not defined.
Issue two: this nested loop seems to complicate the work as it executesconsole.log(i)
more times than it should.
via Wei Zheng
No comments:
Post a Comment