I have a document which has a field as Array of References and it is refering to the same document. I want to populate all the nested fields.
The Data will be Acyclic (person -> children -> children ≠ person).
The Schema of the Document look like
const personSchema = new mongoose.schema({
name: {
type: String,
required: true,
},
[...],
children: [{
type: mongoose.Schema.Types.ObjectId,
ref: "Person",
}],
});
const Person = mongoose.model("Person", personSchema);
while finding a person with his/her _id I want that it’s children, children’s children, …. must be populated
Output should look like
{
"_id": 1,
"name": "John Doe",
"children": [
{
"_id": 2,
"name": "Jane Doe",
"children": [
{
"_id": 3,
"name": "Billy Doe",
"children": [
{
"_id": 4,
"name": "Bobby Doe"
[...]
}
]
}
]
}
]
}
New contributor
TheCodeVenturer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.