查找文档,修改和更新

时间:2018-06-13 18:28:41

标签: node.js mongodb mongoose

我有多个文档需要更新。我发送一系列代码和需要插入或更新的数据。我修改了文档,但是当我想保存时,我收到一个错误,那就是order.save不是一个函数。 我的代码有什么问题?我该怎么做才能纠正它?

产品是一个对象
ordersCode是一个数组

var query = Order.find({ "orderNumber": { $in: ordersCode } }).lean();

query.exec((err, orders) => {
    if (err) {
        return callback(err);
    }

    orders.forEach(order => {
        order.products.forEach(orderProduct => {
            if (orderProduct.code == product.code) {
                orderProduct.quantity = parseInt(orderProduct.quantity) + parseInt(product.quantity);
            } else {
                order.products.push(product);
            }
        });

        order.save((err) => {
            if (err) {
                return callback(err);
            }
        });
    });

    return callback(null, orders);
});

0 个答案:

没有答案
相关问题