我有一个更新商店库存的功能,当商店更新它的库存时,它也应该更新除库存外的所有经销商的库存。
它适用于非变体产品,但如果产品具有变体,信息位于变体数组中,则它不起作用。
我找不到忽略变体数组中的股票字段的方法。
当前代码:
// Update store owners inventory
const product = await Inventory.findOneAndUpdate(
{
_id: req.params.inventoryId,'products._id': req.params.productId,},{
$set: {
'products.$.title': title,'products.$.desc': desc,'products.$.category': category,'products.$.pricing': pricing,'products.$.moq': moq,'products.$.stock': stock,'products.$.media': media,'products.$.shipping': shipping,'products.$.hasvariation': hasvariation,'products.$.variations': variations,{ new: true,runValidators: true }
);
// Update resellers inventory
await Inventory.updateMany(
{
store: req.params.storeId,_id: {
$ne: req.params.inventoryId,runValidators: true }
);
变体数组:
"variations": [
{
"_id": "60fe50c79246f71e00fb94ac","name": "Size","options": [
{
"pricing": [
{
"type": "moqperproduct","prices": {
"retail": "3.00","stockist": "1.00","agent": "2.00"
}
},{
"type": "tieredpricing","prices": [
{
"tier": 1,"min": 1,"max": 9,"value": "40.00"
},{
"tier": 2,"min": 10,"max": 19,"value": "30.00"
},{
"tier": 3,"min": 20,"max": 30,"value": "20.00"
}
]
},{
"type": "totalpurchase","prices": {}
}
],"stock": 50,"_id": "60fe50c79246f71e00fb94ad","name": "Large","moq": [
{
"type": "moqperproduct","options": {
"retail": "1","stockist": "55","agent": "10"
}
}
]
},{
"pricing": [
{
"type": "moqperproduct","_id": "60fe50c79246f71e00fb94ae","name": "Medium","stockist": "125","agent": "21"
}
}
]
}
]
}
]
库存架构:
const InventorySchema = new mongoose.Schema(
{
store: {
type: mongoose.Schema.ObjectId,required: true,ref: 'Store',user: {
type: mongoose.Schema.ObjectId,ref: 'User',products: [
mongoose.Schema(
{
title: {
type: String,required: [true,'Please add a title for your product'],desc: {
type: String,'Please add a description for your product'],category: {
type: String,'Please add a category for your product'],pricing: {
type: [Object],default: [
{
type: 'moqperproduct',prices: {},{
type: 'tieredpricing',prices: [],{
type: 'totalpurchase',],moq: {
type: [Object],stock: {
type: Number,min: 0,media: {
type: [Object],shipping: {
type: Object,hasvariation: {
type: Boolean,default: false,variations: [
mongoose.Schema({
name: {
type: String,options: [
mongoose.Schema(
{
name: {
type: String,pricing: {
type: Object,default: [
{
type: 'moqperproduct',{
type: 'tieredpricing',{
type: 'totalpurchase',moq: {
type: Object,stock: {
type: Number,default: 0,{ minimize: false }
),}),{ minimize: false }
),{ timestamps: true }
);
module.exports = new mongoose.model('Inventory',InventorySchema);
非常感谢有关如何重构模型的任何提示或建议。谢谢