猫鼬以编程方式更新单个数组属性项

时间:2018-08-11 02:24:39

标签: javascript mongodb mongoose

我正在尝试使用猫鼬更新单个数组属性项:

const updatePurchaseItem = async (purchaseOrder, itemIndex, product) => {

    let position = "items." + itemIndex + ".product_id";
    let query = {
            _id: purchaseOrder._id,
            deletedAt: null
    };

    await PurchaseOrderModel.update(query, position: product._id);
}

我遇到以下错误:

Cast to ObjectId failed for value "items.1.product_id" at path "_id" for model "PurchaseOrder"

使此工作有效的代码正确语法是什么?

path "_id"是什么意思?

使用purchaseOrder从数据库中读取productmongoose.findOne()

1 个答案:

答案 0 :(得分:0)

您是否尝试过将参数解析为对象?如{position: product._id}