1
0 Comments

How to find recursive nested documents in mongoose

I'm building a note taking app in which there can be as many nested notes inside one another as the user pleases, So my model for mongoose is like

const noteSchema = new Schema({
icon: {
type: String,
},
banner: {
type: String,
},
name: {
type: String,
required: true,
},
user_id: {
type: String,
required: true,
},
date: {
type: Date,
required: true,
default: Date.now,
},

sub_notes: [this],
path: {
type: String,
required: true,
},
parent: {
type: String,
required: true,
},
content: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "blocks",
},
],
});

I want to find the nested note by id in the sub_notes array or it can be at the top as well, But I'm not sure how to query the data for nested properties if the nesting can be unlimited

on April 4, 2021