I want to toggle the boolean value which is in the array of the schema. I am trying to toggle the value with the value present in the database(refer the code). But it is just turning true to false, but not false to true.
Following is the code
exports.postStatusItemchange = (req, res, next) => {
const productId = req.body.productId;
const itemId = req.body.itemId;
Product.updateOne({_id: productId, "items._id": itemId}, {
$set: {
"items.$.status": !"items.$.status"
}
}).then((result) => {
console.log(result);
res.send("done");
}).catch(err => {
res.send(err);
})
}
Following is the schema for the reference
const productSchema = new Schema({
name: {
type: String,
required: false,
},
status: {
type: Boolean,
required: false,
default: true,
},
items: [
{
foodName: {
type: String,
required: true,
},
price: {
type: Number,
required: true,
},
status: {
type: Boolean,
default: true
}
},
],
});
I am expecting that the value in the status field gets toggle whenever the API is ran. Thank you for your time.