Создайте документ mongodb с ключами объекта с именем uuid

Я хочу создать документ, как показано ниже, в мангусте. Существует объект pageElements с ключами объекта uuid внутри.

{
    "pageElements": {
        "fc6b151-cd5-3c1-0e00-241b4411d4eb": {
            "containers": {
                "de325a-acb2-e0a1-e521-20b3e81e33f": {
                    "components": [{
                        ...
                    }],
                }
            },
        },
        "075f7c-5aff-c850-a731-a8dff1bea5f8": {
            "containers": {
                ...
            }
        }
    },
}

Я попытался создать схему следующим образом:


const componentSchema = new mongoose.Schema(
  {
    ...
  },
);

const containersSchema = new mongoose.Schema(
  {
    components: [{ type: componentSchema, required: true }],
  },
);

const pageElementsSchema = new mongoose.Schema(
  {
    containers: { type: containersSchema, required: true },
  },
);

const pageSchema = new mongoose.Schema(
  {
    pageElements: {
      type: pageElementsSchema,
    },
  },
);

Но новый созданный документ содержит только пустой объект pageElements.

Использование JavaScript и MongoDB
Использование JavaScript и MongoDB
Сегодня я собираюсь вкратце рассказать о прототипах в JavaScript, а также представить и объяснить вам работу с базой данных MongoDB.
2
0
16
1
Перейти к ответу Данный вопрос помечен как решенный

Ответы 1

Ответ принят как подходящий

Используйте карта как

const componentSchema = new mongoose.Schema(
  {
    ...
  },
);

const containersMapSchema = new mongoose.Schema(
  {
    components: [{ type: componentSchema, required: true }],
  },
);

const pageElementsMapSchema = new mongoose.Schema({
  containers: { 
    type: Map,
    of: containersMapSchema
  }
})

const pageSchema = new mongoose.Schema(
  {
    pageElements: {
      type: Map,
      of: pageElementsMapSchema
    },
  },
);

Другие вопросы по теме