Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在MongoDB中更新布尔值_Javascript_Node.js_Mongodb_Boolean_Mern - Fatal编程技术网

Javascript 如何在MongoDB中更新布尔值

Javascript 如何在MongoDB中更新布尔值,javascript,node.js,mongodb,boolean,mern,Javascript,Node.js,Mongodb,Boolean,Mern,我有一个boolean,我需要在mongoDB中更新它,这取决于用户/餐厅是否处于活动状态。有没有办法做到这一点?这就是我目前所拥有的 模式-简化 const ownerSchema = new Schema({ name: { type: String, required: true }, foodType: { type: String, required: true }, location: { type: Array, default: [] }, active: {

我有一个
boolean
,我需要在
mongoDB
中更新它,这取决于用户/餐厅是否处于活动状态。有没有办法做到这一点?这就是我目前所拥有的

模式-简化

const ownerSchema = new Schema({
  name: { type: String, required: true },
  foodType: { type: String, required: true },
  location: { type: Array, default: [] },

  active: { type: Boolean, default: false },
});
和控制器:

  isActive: function(req, res) {
console.log(req.body);
db.Owners.updateOne({ _id: req.params.id })
  .then(dbModel => res.json(dbModel))
  .catch(err => res.status(422).json(err));
 },
我只需要一个简单的方法来改变它的真假。任何帮助都将不胜感激

试试看

db.Owners.update({ _id: req.params.id },{"$set":{"active":false}})
  .then(dbModel => res.json(dbModel))
  .catch(err => res.status(422).json(err));
 },

这很有效,谢谢!是否有任何方法可以生成某种形式的if语句,因此,如果它为false,它将变为true,如果它为true,它将变为false,或者我必须单独执行它们?您需要使用逻辑单独处理它,而mongo update只是更新。如果答案有帮助,请打分并投票