我可以更新猫鼬api回调函数中Collection.find()找到的对象吗

时间:2019-01-31 14:43:27

标签: node.js mongodb rest

我可以将currentUser的猫鼬对象更新为else块 我的代码如下:

<?php while($query->fetch()) { ?>
    <h2 class="card-title"><?php echo $title ?></h2>
    <p class="card-text"><?php echo $body ?></p>
    <a class="btn btn-CMD" href="#">Read More &rarr;</a>
<?php } ?>

1 个答案:

答案 0 :(得分:0)

是的,只需更新要更新的任何字段,然后调用'.save()'方法

Profile.findOne({ _id: req.body.id }).then((currentUser) => {
  if (currentUser.password == req.body.newPassword) {
    res.status(200).json({
      "message": "similar to old password"
    });
  }
  else {
    // update here
    currentUser.name = "test";
    // save to DB
    currentUser.save()
    res.status(200).json({
      "message": "password got updated"
    });
  }
});

详细了解here