Как апдейтить вложенные элементы в массиве вложенных элементов в базе данных mongo.db?
Предположим у меня есть массив данных, включающих файлы и папки. В каждой Папке содержатся 2 вложенных массива: один из файлов, другой - из папок. В каждой папки последнего вложенного массива также содержатся папки, в которых... и так далее. Древовидная структура.
Допустим я удаляю элемент в папке с определенной вложенностью на фронте. Как мне правильно обновить свою структуру данных, чтоб изменения сохранились?
Я использую mongoose если что
Массив данных (пока в typescript) выглядит вот так:
private bookshelf: any = [
{
id: '1',
name: 'Im Westen nichts Neues',
description: 'The book by author Eirch Maria Remarque',
imageLink:
'https://img.yakaboo.ua/media/catalog/product/cache/1/image/398x565/234c7c011ba026e66d29567e1be1d1f7/7/4/74431_60714327.jpg',
},
{
id: '2',
name: 'Der Funke Leben',
description: 'The book by author Eirch Maria Remarque',
imageLink:
'https://images-na.ssl-images-amazon.com/images/I/71-dnZuZ0pL.jpg',
},
{
id: '3',
name: 'Books',
isDeletable: true,
includedFiles: [
{
id: '4',
name: 'Der Funke Leben',
description: 'The book by author Eirch Maria Remarque',
imageLink:
'https://images-na.ssl-images-amazon.com/images/I/71-dnZuZ0pL.jpg',
},
],
includedFolders: [
{
id: '5',
name: 'Thrillers',
isDeletable: true,
includedFiles: [
{
id: '6',
name: 'Der Funke Leben',
description: 'The book by author Eirch Maria Remarque',
imageLink:
'https://images-na.ssl-images-amazon.com/images/I/71-dnZuZ0pL.jpg',
},
{
id: '7',
name: 'Der Funke Leben',
description: 'The book by author Eirch Maria Remarque',
imageLink:
'https://images-na.ssl-images-amazon.com/images/I/71-dnZuZ0pL.jpg',
},
{
id: '8',
name: 'Der Funke Leben',
description: 'The book by author Eirch Maria Remarque',
imageLink:
'https://images-na.ssl-images-amazon.com/images/I/71-dnZuZ0pL.jpg',
},
{
id: '9',
name: 'Der Funke Leben',
description: 'The book by author Eirch Maria Remarque',
imageLink:
'https://images-na.ssl-images-amazon.com/images/I/71-dnZuZ0pL.jpg',
},
],
includedFolders: [
{
id: '10',
name: '2007',
isDeletable: true,
includedFiles: [
{
id: '11',
name: 'Der Funke Leben',
description: 'The book by author Eirch Maria Remarque',
imageLink:
'https://images-na.ssl-images-amazon.com/images/I/71-dnZuZ0pL.jpg',
},
{
id: '12',
name: 'Der Funke Leben',
description: 'The book by author Eirch Maria Remarque',
imageLink:
'https://images-na.ssl-images-amazon.com/images/I/71-dnZuZ0pL.jpg',
},
],
includedFolders: [],
},
],
},
],
},
];
Фронт выглядит как-то так
Как правильно построить запрос "remove selection"?
Ответы (1 шт):
Все зависит от контекста
1. Если это POC или тестовое задание тогда можно было бы обходить "документы" рекурсивно при этом выстраивая "путь" из id. Но єто путь в некуда т.к. монго документ имеет ограниченный размер, не более 16Мб и по закону Мерфи в системе появится хотя бы один "жирный" каталог и тогда могут начаться проблемы.
Проще и правильнее 2. Ознакомиться с подходами хранения древовидных структур в mongodb и просто переделать солюшн пока не поздно.
