I’m trying to create a method for updating a single document.
The following is my query:
const {
assetId,
editAssetData,
} = req.body;
const newAsset = await Catalog.findOneAndUpdate(
{ 'assets._id': assetId },
{
$set: {
assetName: editAssetData.assetName,
assetQuantity: editAssetData.assetQuantity,
singleQuantityPrice: editAssetData.singleQuantityPrice,
totalQuantityPrice: editAssetData.totalQuantityPrice,
},
}, (err, asset) => { (err) ? console.log('err=>', err) : console.log('asset=>', asset); },
);
const asset = await newAsset.save();
console.log(asset) just returns the catalog document, to which the asset is a nested element.
This is what my model looks like
const catalogSchema = new Schema({
assetType: { type: String, required: true },
description: { type: String, required: false },
creator: { type: Schema.Types.ObjectID, required: true, ref: 'User' },
assets: [{
assetName: { type: String, required: false },
assetQuantity: { type: Number, required: false },
singleQuantityPrice: { type: Number, required: false },
totalQuantityPrice: { type: Number, required: false },
}],
});