Recursive mongoose schema

81 views
Skip to first unread message

Francesca Ricci-Tam

unread,
Aug 9, 2021, 7:26:36 PM8/9/21
to Mongoose Node.JS ODM
Dear Mongoose experts,

is it possible to write a mongoose schema where one of the fields is allowed to be another instance of said schema? I have attached a doodle to illustrate the kind of structure that I'm aiming for.
In the simplest case, the schema named "Branch" has two fields, "name" and "id". However, if the second field is not "id", that second field can instead be called "branch" and will be another instance of Branch, which comes with its own two fields -- "name" and either "id" or another "branch", and so forth. I don't want the structure to be fixed, either -- in the data that I'll be reading out, some instances of this Branch data model might have no branching at all (just well-behaved "name" and "id" fields), while some might have one or more branches, and I want to be able to pick up all cases of this type of structure.
Can one define a schema field in a recursive manner like this?
Thanks,

Francesca Ricci-Tam
example.jpg
Reply all
Reply to author
Forward
0 new messages