Building Mongoose Schema with indeterminate number of nested objects -
1. sample dashboard menu data:
const dashboards = [ {"dashboard": "sample", "items": [ { "title": "title 1" }, { "title": "title 2", "items": [ { "title": "title 2-1", "items": [ { "title": "title 2-1-1" }, { "title": "title 2-1-2" } ] }, { "title": "title 2-2" } ] }, { "title": "title 3", "items": [ { "title": "title 3-1" }, { "title": "title 3-2", "items": [ { "title": "title 3-2-1" }, { "title": "title 3-2-2" } ] } ] }, { "title": "title 4", "items": [ { "title": "title 4-1" }, { "title": "title 4-2", "items": [ { "title": "title 4-2-1" }, { "title": "title 4-2-2", "items": [ { "title": "title 4-2-2-1" }, { "title": "title 4-2-2-2" } ] } ] } ] } ]} ];
2. schema attempt:
const dashboardmenuschema = new schema({ title: {type: string}, items: [{ title: string }] }, { _id: true }); dashboardmenuschema.add({ items: [dashboardmenuschema.items] });
my schema not working expected.
what proper approach satisfy , indeterminate number of menu items?
thanks in advance help.
turns out pretty straight forward ....
const itemschema = new mongoose.schema({ title: {type: string}, }); export const dashboardmenuschema = new mongoose.schema({ title: {type: string}, items: [itemschema] }, { _id: true }); dashboardmenuschema.add({ items: [dashboardmenuschema.items] });
Comments
Post a Comment