如何解决猫鼬:更新数组中的对象批量写入
我正在尝试更新数组中的对象。
我的字段以前不是数组,我曾经使用此代码更新我的字段:
exports.updateStock = (req,res,next) => {
console.log(req.body.order.products);
let myOperations = req.body.order.products.map((prod) => {
return {
updateOne: {
filter: { _id: prod._id },update: { $inc: { stock: -prod.quantity,sold: +prod.quantity } },},};
});
Product.bulkWrite(myOperations,{},(err,products) => {
if (err) {
console.log("im here",err);
return res.status(400).json({
error: "Bulk operation failed",});
}
next();
});
};
字段:
stock: [
{
size: {
type: String,required: true,stock: {
type: Number,],
我知道如何根据尺寸更新stock.stock吗? 尺寸与产品-> prod.size
一起传递示例:
in store i have size : M | 100 stocks
L | 150 stocks
XS| 10 stocks
user : want to buy size M and quantity 2
so after the order i have to update Size m stocks
100-2 = 98 stocks remaing after order :
M | 98 stocks
L | 150 stocks
XS| 10 stocks
我的整个产品架构:
const mongoose = require("mongoose");
const { ObjectId } = mongoose.Schema;
const productSchema = new mongoose.Schema(
{
name: {
type: String,trim: true,maxlength: 32,description: {
type: String,maxlength: 2000,discountprice: {
type: Number,price: {
type: Number,category: [
{
type: ObjectId,ref: "Category",subcategory: [
{
type: ObjectId,ref: "SubCategory",stock: [
{
size: {
type: String,sold: {
type: Number,default: 0,photo: [String],{ timestamps: true }
);
module.exports = mongoose.model("Product",productSchema);
解决方法
您可以使用arrayfilters在数组g++.exe -g CLClockv2.cpp -o CLClockv2.exe
中的-l
的基础上更新-L
,
stock
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。