首页 > 解决方案 > 如何在模式中创建嵌套字段?

问题描述

假设我有这个用户模式,它有一个带有多个插槽的库存。我如何将这些 slotsimg 和 slottext 字段嵌套在库存字段中?

例如像这样:

const UserSchema = new mongoose.Schema({
  Inventory: {
    slot1img: {
      type: String,
    },
    slot1text: {
      type: String,
    },
    slot2img: {
      type: String,
    },
    slot2text: {
      type: String,
    },
    slot3img: {
      type: String,
    },
  },
});

但是,如果我这样尝试,它根本不会出现在数据库中,那么我该怎么做呢?

标签: javascriptmongodbnestedschemafield

解决方案


我找到了一个解决方案:https ://docs.mongodb.com/manual/tutorial/model-embedded-one-to-many-relationships-between-documents/#embedded-document-pattern 。如果其他人遇到此问题,请使用链接:)


推荐阅读