@asdqwee
Front-end developer

Как получить элементы из дочерней модели и добавить их в родительскую?

Родительская схема
const taskSchema = new Schema({
  title: {
    type: String,
    required: true
  },
  startDate: {
    type: Date,
    required: true
  },
  description: {
    type: String,
  },
  dateOfCreation: {
    type: Date,
    default: Date.now
  },
  dateOfUpdate: {
    type: Date,
  },
  status: {
    type: String,
    enum: ['inactive', 'active', 'done'],
    default: 'inactive'
  },
  subtasks: [{
    type: Schema.Types.ObjectId,
    ref: 'subtasks'
  }]
});
module.exports = mongoose.model('tasks', taskSchema);


Дочерняя схема
const subtaskSchema = new Schema({
  title: {
    type: String,
    required: true
  },
  parentId: {
    ref: 'tasks',
    type: Schema.Types.ObjectId,
    required: true
  },
  startDate: {
    type: Date,
    required: true
  },
  description: {
    type: String,
  },
  dateOfCreation: {
    type: Date,
    default: Date.now
  },
  dateOfUpdate: {
    type: Date,
  },
  status: {
    type: String,
    enum: ['inactive', 'active', 'done'],
    default: 'inactive'
  }
});
module.exports = mongoose.model('subtasks', subtaskSchema);


Как на выходе получить tasks c subtasks внутри?
Например:

{
dateOfCreation: "2019-03-11T14:12:52.383Z"
description: "desc!!!222asdasd"
startDate: "2019-03-11T10:00:49.669Z"
status: "inactive"
subtasks: [ МАССИВ ИЗ SUBTASKS ]
title: "4444 edited sss"
}
  • Вопрос задан
  • 30 просмотров
Пригласить эксперта
Ответы на вопрос 1
@alikenski
Fullstack Web Developer
Прочитайте про deepPopulate
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы