我有一个函数来更新子文档中的某些值,但是我在更新数据时遇到了麻烦
let query = InvoiceModel.find({
is_draft:false
products.product: productid
})
query.exec(function (err, data) {
if (data) {
data.forEach(function(si) {
let saleinvoice_id = si._id
console.log(saleinvoice_id);
InvoiceMovement(invoice_id, si)
})
}
})
在InvoiceMovement函数中,我搜索集合PRODUCT中的记录进行更新,并尝试更新数组,如下所示:
productModel.findOneAndUpdate({
_id: product_id,
'warehouses.warehouse': warehouse_id
}, {
$set: {
"warehouses.$.stock": stock_data,
}
}, function (errwu, wupdated) {
if (errwu) {
console.log(errwu);
}
if (wupdated) {
console.log(wupdated);
}
})
但并非所有数据都已处理完毕。
对于特定ID,我有4张发票记录,但只有两张或更少会影响产品集合中要更新的值
我尝试使用async.forEach,但我得到的结果相同。
更新
架构:
发票
const InvoiceSchema = Schema({
invoice_number: {
type: String,
},
products: [{
product: {
type: Schema.Types.ObjectId,
ref: 'products',
required: [true, 'Producto no puede estar vacio']
},
warehouse: {
type: Schema.Types.ObjectId,
ref: 'warehouses'
},
work_order: {
type: Schema.Types.ObjectId,
ref: 'work_orders'
},
quantity: {
type: Number,
required: [true, 'Cantidad no puede estar vacio']
},
base_price: {
type: String,
required: [true, 'Valor Unitario no puede estar vacio']
},
sale_price: {
type: String,
required: [true, 'Valor Unitario no puede estar vacio']
},
discount: {
type: String,
default: "0.00"
},
subtotal: {
type: String,
required: true
},
tax: {
type: String,
required: true
},
total: {
type: String,
required: true
},
}]
}, {
timestamps: true
});
产品
const productSchema = Schema({
code: {
type: String,
index: true
},
name: {
type: String,
index: true,
required: [true, 'Nombre no puede estar vacio']
},
description: {
type: String,
required: [true, 'Descripcion no puede estar vacio']
},
unit: {
type: String,
index: true,
required: [true, 'Unidad no puede estar vacio']
},
exempt: {
type: Boolean,
default: false
},
product_category: {
type: Schema.Types.ObjectId,
ref: 'product_categories',
required: [true, 'Categoría de Producto es requerida']
},
base_price: {
type: String,
default: "0.00"
},
unit_cost: {
type: String,
default: "0.00"
},
warehouses: [
{
warehouse: {
type: Schema.Types.ObjectId,
ref: 'warehouses',
required: [true, "Seleccione una caracteristica"]
},
stock: {
type: Number,
},
unit:{
type: String
}
}
],
subproducts: [
{
subproduct: {
type: Schema.Types.ObjectId,
ref: 'characteristics',
// required: [true, "Seleccione una caracteristica"]
},
}
],
stock: {
type: Number,
default: 0
}
}, {timestamps: true});
从发票架构中,我通过产品阵列获取产品ID和数量,并使用该数据更新PRODUCT架构内仓库阵列中的库存。 我需要为每个发票执行此操作。 一张发票可以注册许多产品
答案 0 :(得分:0)
以下是您可以执行此操作的一种方法。但是,如果您使用mongoose.promise
,这可能会更容易。
InvoiceModel.find({}, 'products', function(err, invoice) {
if(err) throw err
const products = invoice.products
// Loop through all the objects in the array
products.forEach(function(productsObj) {
// Get the product model for each product
ProductModel.findById(productsObj.product, 'warehouses', function(err, product) {
if(err) throw err
// Get a new array of only ids
const warehousesIds = product.warehouses.map(warehousesObj => {
return warehousesObj.warehouse
})
// Get the index of the current warehouse in the warehouses array in your product model
const warehouseIndex = warehousesIds.indexOf(productsObj.warehouse)
// Check if warehouse exists and if so assign the new value
if(warehouseIndex > -1) product.warehouses[warehouseIndex].stock = productsObj.quantity
// Save your model
product.save()
})
})
})
希望它有所帮助!