如何更新mongodb中的特定刺痛?

时间:2018-04-04 01:03:10

标签: mongodb meteor

我有一张"地图"基于表格。

enter image description here

当用户点击地图时,地图会更新

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

功能:

  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} });
  }

点击几下DB中的记录后会看到:

enter image description here

我如何改变所有&#34; 1&#34;为空值和&#34; 0&#34;到&#34; 5&#34;?

2 个答案:

答案 0 :(得分:0)

您可以在$运算符或$ elemMatch上使用点表示法。问题是只会更新第一次出现。 您还可以迭代文档并更新值

select MATNR, HKL from(
    select MATNR, HKL
      from BESTAND
     where LOK_NR >1
       and BEST_FREI =0
     union
    select MATNR, HKL from BESTAND
     where LOK_NR =1
      and BEST_FREI >0
)
 WHERE MATNR NOT IN(
    select MATNR, HKL
      from SONDER_AUFTRAG
     where GREIFLOK = 'J'
)
 GROUP BY MATNR, HKL
 ORDER BY MATNR, HKL;

答案 1 :(得分:0)

回复@Graciano的代码应修改为:

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} }); 
});

将所有出现的'1'替换为null你应该在替换函数中使用RegExp而不是字符串,如下所述:

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