I have two Schemas
var recipeSchema = new mongoose.Schema({
name: String,
products: [{
product: Product,
weight: Number
}]
})
var productSchema = new mongoose.Schema({
name: String,
weight: Number,
price: Number
})
module.exports.updateProduct = function(req, res){
var productId = req.params.productId;
Product
.findById(productId)
.exec(function(err, product){
if(err){
console.log("Error finding product");
res
.status(500)
.json(err);
} else if (!product){
res
.status(404)
.json({
"message": "Product ID not found"
});
} else {
product.name = req.body.name;
product.weight = parseInt(req.body.weight);
product.price = parseFloat(req.body.price).toFixed(2);
product.save(function(err, updatedProduct){
if(err){
res
.status(500)
.json(err);
} else {
res
.status(204)
.json();
}
})
}
})
}
I want to update Product in two documents at once. I wrote piece of code which update Product in products collection. But I don't know how to update Recipe's Product.
For example: Recipe:
{
"_id" : ObjectId("596d281a4b49abec4abd3d48"),
"name" : "Cake",
"products" : [
{
"product" : {
"_id" : ObjectId("596d27a24b49abec4abd3d3c"),
"name" : "sugar",
"price" : 12.34
},
"weight" : 100
}
]
}
Product
{
"_id" : ObjectId("596d27a24b49abec4abd3d3c"),
"name" : "sugar",
"price" : 12.34
}
I want to update Product and Recipe's product at once. Is it possible?