根据其他收藏品在阵列中减去价值

发布于 2025-01-25 06:26:17 字数 2669 浏览 2 评论 0原文

我有以下收集

    const TransactionSchema = mongoose.Schema({
        schedule: {
            type: mongoose.Schema.ObjectId,
            required: true,
            ref: "Schedule"
        },
        uniqueCode: {
            type: String,
            required: true
        },
item: {
            type: mongoose.Schema.ObjectId,
            required: false,
            ref: "Item"
        },
        created: {
            type: Date,
            default: Date.now
        },
    
        status: {
            type: String,
            required: false
        },
    })

时间表,

const ScheduleSchema = mongoose.Schema({
start: {
    type: Date,
    required: true,
},
end: {
    type: Date,
    required: false,
},
questions: {
    type: Array,
    default: [],
},
items: [{
    item: {
        type: mongoose.Schema.ObjectId,
        require: true,
        ref: "Item"
    },
    stock: {
        type: Number,
        required: true
    }
}],

status: {
    type: String,
    required: false
},
})

我想返回该项目在交易中出现的次数,并用计划收集中的对象项目中的总项目数量减少。

例如,我有以下数据:

交易

 [
   {
      "_id":ObjectId('626134d547c28b6ecc6d0c6c'),
      "schedule":624edc44ac2edc1cf07821de,
      "uniqueCode":"312312312312",
      "created":2022-04-21T10:41:25.901+00:00,
      "item": 61efd8a812432135c08a748d,
      "status": "Active"
   },
   {
      "_id":ObjectId('62615ea3db7d00061b7cc437'),
      "schedule":624edc44ac2edc1cf07821de,
      "uniqueCode":"1213123123",
      "created":2022-04-21T13:39:47.351+00:00,
      "item": 624c6b26a41c439987b1eb42,
      "status": "Active"


   }
]

计划

[
   {
      "_id":ObjectId('624edc44ac2edc1cf07821de'),
      "start":2000-01-01T00:00:00.000+00:00,
      "end":2000-01-01T00:00:00.000+00:00,
      "questions":[
         12,
         32,
         122
      ],
      "items":[
         {
            "item":61efd8a812432135c08a748d,
            "stock":120
         },
         {
            "item":624c6b26a41c439987b1eb42,
            "stock":1000
         }
      ],
      "status":"Active"
   },
]

项目

[
   {
      "_id": ObjectId('61efd8a812432135c08a748d'),
      "name": "Samsung S21"
      
   },
   {
     "_id": ObjectId('624c6b26a41c439987b1eb42'),
     "name": "Iphone 10"
     
   }
]

我想获得以下结果:

[
   {
      "item":"Samsung S21",
      "total":119
   },
   {
      "item":"Iphone 10",
      "total":999
   }
]

注意:我希望查询仅查询一个时间表数据,以便结果仅显示该时间表中的项目。第一行显示了项目120-1的总库存119,这是该项目在交易中出现的次(状态处于活动状态)。第二行显示999,因为该项目已出现在一个交易中。

谢谢。对不起,我的英语不好。

I have the following collections

    const TransactionSchema = mongoose.Schema({
        schedule: {
            type: mongoose.Schema.ObjectId,
            required: true,
            ref: "Schedule"
        },
        uniqueCode: {
            type: String,
            required: true
        },
item: {
            type: mongoose.Schema.ObjectId,
            required: false,
            ref: "Item"
        },
        created: {
            type: Date,
            default: Date.now
        },
    
        status: {
            type: String,
            required: false
        },
    })

schedule

const ScheduleSchema = mongoose.Schema({
start: {
    type: Date,
    required: true,
},
end: {
    type: Date,
    required: false,
},
questions: {
    type: Array,
    default: [],
},
items: [{
    item: {
        type: mongoose.Schema.ObjectId,
        require: true,
        ref: "Item"
    },
    stock: {
        type: Number,
        required: true
    }
}],

status: {
    type: String,
    required: false
},
})

I want to return how many times the item appear in transaction and reduce it with the number of total item I have in array of objects items in schedule collection.

For example I have the following data:

transaction

 [
   {
      "_id":ObjectId('626134d547c28b6ecc6d0c6c'),
      "schedule":624edc44ac2edc1cf07821de,
      "uniqueCode":"312312312312",
      "created":2022-04-21T10:41:25.901+00:00,
      "item": 61efd8a812432135c08a748d,
      "status": "Active"
   },
   {
      "_id":ObjectId('62615ea3db7d00061b7cc437'),
      "schedule":624edc44ac2edc1cf07821de,
      "uniqueCode":"1213123123",
      "created":2022-04-21T13:39:47.351+00:00,
      "item": 624c6b26a41c439987b1eb42,
      "status": "Active"


   }
]

schedule

[
   {
      "_id":ObjectId('624edc44ac2edc1cf07821de'),
      "start":2000-01-01T00:00:00.000+00:00,
      "end":2000-01-01T00:00:00.000+00:00,
      "questions":[
         12,
         32,
         122
      ],
      "items":[
         {
            "item":61efd8a812432135c08a748d,
            "stock":120
         },
         {
            "item":624c6b26a41c439987b1eb42,
            "stock":1000
         }
      ],
      "status":"Active"
   },
]

Item

[
   {
      "_id": ObjectId('61efd8a812432135c08a748d'),
      "name": "Samsung S21"
      
   },
   {
     "_id": ObjectId('624c6b26a41c439987b1eb42'),
     "name": "Iphone 10"
     
   }
]

I want to get the following result:

[
   {
      "item":"Samsung S21",
      "total":119
   },
   {
      "item":"Iphone 10",
      "total":999
   }
]

Note: I want the query to query one schedule data only so the result only shows the items in that schedule. The first row shows 119 from total stock of item 120 - 1 which is how many times the item appeared in transaction (where the status is active). The second row shows 999 because the item has appeared in one transaction.

Thank you. Sorry for my bad English.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文