You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在数组对象中存储数组?API代码解决方案及修改说明

实现data字段存储嵌套数组的方案

一、Mongoose模型修改

原模型中data字段仅定义为普通数组,未明确嵌套结构,为规范存储嵌套数组,需细化data的结构定义:

const serviceAndProductSchema = new mongoose.Schema({
    index: {
        type: Number,
        required: false
    },
    previousHash: {
        type: String,
        required: false
    },
    hash: {
        type: String,
        required: false
    },
    data: [{
        // 保留原data数组元素的字段
        serviceName: String,
        serviceId: mongoose.Schema.Types.ObjectId,
        title: String,
        text: String,
        imageName: String,
        imageUrl: String,
        slug: String,
        // 新增嵌套数组字段,用于存储dot相关子数据
        dots: [{
            dotId: mongoose.Schema.Types.ObjectId,
            dotName: String
        }]
    }],
}, { timestamp: true });

修改说明:

  • 将data从单纯的Array类型改为对象数组,明确每个元素的字段结构
  • 在data的对象结构中新增dots字段,类型为嵌套数组,专门存储dotId、dotName这类子数据

二、接口代码修改

1. 新增数据逻辑修正

原代码新增Block时,直接插入无键名的数组属于语法错误,需给嵌套数组指定对应模型的键名(dots):

// 原错误代码段
logCoin.addNewBlock(
    new Block(0, new Date().toLocaleString(), {
        serviceName: serviceName,
        serviceId: mongoose.Types.ObjectId(serviceId),
        title: title,
        text: text,
        imageName: req.file.filename,
        imageUrl: req.fileurl,
        slug: slugData,
        [{
            dotId: mongoose.Types.ObjectId(dotId),
          dotName: dotName
        }],
    })
);

// 修改后代码段
logCoin.addNewBlock(
    new Block(0, new Date().toLocaleString(), {
        serviceName: serviceName,
        serviceId: mongoose.Types.ObjectId(serviceId),
        title: title,
        text: text,
        imageName: req.file.filename,
        imageUrl: req.fileurl,
        slug: slugData,
        // 指定嵌套数组键名dots,支持传入多个dot对象
        dots: [{
            dotId: mongoose.Types.ObjectId(dotId),
            dotName: dotName
        }]
    })
);

2. 更新嵌套数组的扩展逻辑(可选)

如果需要更新dots中的特定元素,可使用Mongoose的arrayFilters精准定位:

// 示例:更新data数组中指定title元素下的特定dot
let updateDotData = await ServiceAndProduct.updateOne(
    { _id: id, "data.title": oldTitle, "data.dots.dotId": targetDotId },
    {
        $set: {
            "data.$.dots.$[dot].dotName": newDotName
        }
    },
    {
        arrayFilters: [{ "dot.dotId": targetDotId }]
    }
);

修改说明:

  • 新增数据时必须给嵌套数组指定对应键名,避免语法错误和数据结构混乱
  • 更新嵌套数组时,arrayFilters可精准定位子数组元素,避免误改其他数据

三、注意事项

  • 前端请求体中嵌套数组数据的键名需与模型、接口定义一致(即dots)
  • 若需支持多个dot数据,前端直接传入数组格式的dots即可,接口无需额外处理
  • 若不需要严格字段验证,也可保留data为普通数组,但明确结构更利于后续维护

内容的提问来源于stack exchange,提问作者sAcHiN pAtEl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 04:15:42