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只是更新。如果答案有帮助,请打分并投票