0

I have a "map" based on a table.

enter image description here

when user click on map, the map updates

if (value === null) return (
  <td onClick={this.handleCellClick.bind(this, row, col)}></td>
);

the function:

  handleCellClick(row, col) {
    let currentPlayer = this.currentPlayer(); // 1
    let game = this.props.game;
    game.board[row][col] = currentPlayer;
    Games.update(game._id, { $set: {board: game.board} });
  }

After few click the record in DB looks this:

enter image description here

How I can change all "1" to null values and "0" to "5"?

Michel Floyd
  • 18,793
  • 4
  • 24
  • 39
Duntik
  • 27
  • 1
  • 6

2 Answers2

0

You can use dot notation on the $ operator or $elemMatch. The problem is that will only updates the first occurrence. You can also iterate over the document and update the values

db.collection.find({_id:'3838383'}).forEach(function(doc {
  //iterate over your array and update the values
  var board = doc.board;
  for(var i in board) { //not sure if u have 1/2 dimension array
    if(board[i] = 0){board[i] = 5;}
  }
  db.collection.update({_id:'3838383'}, {$set:{'board': board}});
}))
Graciano
  • 508
  • 4
  • 11
  • I modify your code, but still not updating anything. Games.find(game._id).forEach(function(doc) { let tast2 = doc.board.toString(); tast2 = tast2.replace('1', null); alert(doc.board); Games.update(game._id, { $set: {board: tast2} }); }); – Duntik Apr 04 '18 at 02:07
0

The code u replied with to @Graciano should be modified to:

Games.find(game._id).forEach(function(doc) { 
    let tast2 = doc.board.toString(); 
    tast2 = tast2.replace(new RegExp('1', 'g'), null); 
    alert(doc.board); 
    Games.update(game._id, { $set: {board: tast2} }); 
});

to replace all occurences of '1' to null u should use a RegExp not a string in the replace function as stated here:

https://stackoverflow.com/a/17606289/3565352

Abkreno
  • 153
  • 1
  • 8