[英]Unable to add new item to MongoDB array
我正在使用以下代碼更新集合中的現有文檔。我想將新項目推送到數組中,但是腳本既不會引發異常也不會向數組添加任何內容。
要求專家建議解決問題。
transportModel.findOne({ "name": req.body['providerName'], "contact.postalCode": parseInt(req.body['postalCode']) },function (err, doc) {
if (err) {
logger.error("Error while updating record : - " + err.message);
} else if (doc === null) {
logger.error("Error while updating record in transport details : - unable to update database");
} else {
doc.contact.addressLine1= req.body['addressLine1']
doc.contact.addressLine2= req.body['addressLine2']
//An array in transportModel.
//Add new items to array
doc.vehicle.push({
vehicleType:req.body['vehicleType'],
})
doc.save()
}
});
嘗試$push
方法:
transportModel.update(
{ ...your query here },
{ $push: { vehicle:{ vehicleType:req.body['vehicleType'] } } },
done
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.