How to update array value in mongoose ?
This is my schema
var cars = new Schema({
brand : String,
prices:[ {color : String, price: String}]
}
let carID = req.body.carID;
let brandPriceArraySingleID = req.body.brandPriceArraySingleID;
var ObjectId = require('mongoose').Types.ObjectId;
var query = { 'prices.id' : new ObjectId(brandPriceArraySingleID) };
let carsPriceUpdate = await cars.findOneAndUpdate(query, { $set: { "price": "new price" } });
This above code is not working!!
Questions
- How to find and update array value?
- Do I need to check whether exist carID in cars model before array value find?
brandPriceArraySingleIDand what do you want to update? All prices? An specific price for a color in a brand?