Как получить название модели / схемы из вложенного документа? - PullRequest
0 голосов
/ 21 апреля 2020

У меня есть следующая схема и подсхема:

Дочерняя схема:

const SettingSchema = new Schema({
   settingIdentifier: {
      type: String,
      required: true
   },
   shareLink: {
      type: String,
      required: true
   },
   downloadLink: {
      type: String,
      required: true
   },
   direction: {
      type: String,
      required: true
   }

});

И родительская схема:

const DeviceSchema = new Schema({
    id: {
      type: String,
      required: true
    },
    store: {
      type: String,
      required: true
    },
    storeName: {
      type: String,
      required: true
    },
    deviceInfo: {
      type: String,
      required: true
    },
    contentSettings:{
      type:[SettingSchema],
      required: true
    }
})

В моем коде я хочу получить имя дочерней схемы из найденного документа. Например, я могу получить документ следующим образом:

Device.findOne({ deviceInfo: "someValue" }).then(device => {
    // device is the retrieved document
    // with this device object, how do I get the device[settings] schema name?
    // device[settings] is an array of SettingSchema with possibly multiple members
    // Expected output : "SettingSchema"
});

Итак, как мне получить имя схемы вложенного документа «настройки», когда у меня есть объект «устройство»?

Заранее большое спасибо.

Редактировать: я прилагаю файл console.log, чтобы показать, какое устройство содержится после Device.findOne.

{
        "_id": "5ea034d07d865e38c0d4ed6f",
        "id": "1a89d59800a72e39",
        "store": 1,
        "storeName": "Singapore Store",
        "deviceInfo": "test device2",
        "contentSettings": [
            {
                "settingIdentifier": "NewYearEvent",
                "shareLink": "share1",
                "downloadLink": "dl1",
                "direction": "D"
            },
            {
                "settingIdentifier": "EasterEvent",
                "shareLink": "share2",
                "downloadLink": "dl2",
                "direction": "H"
            }
        ],
        "__v": 0
    }

Итак, Device.findOne получает экземпляр устройства из база данных, и она содержит данные, но я не вижу метаданных, чтобы определить тип или имя схемы самого устройства или его членов

...