Traverse the mongo DB scheme in Mongoose JS
-
21-12-2019 - |
سؤال
I'm trying to find out if Mongoose.JS exposes subDocuments with in the .modelSchema. The basic idea is that I want to generate a tree view of my database model.
For Exampe I a status schema that allow each status to have an array of questions that are made from a Question Schema. My Mongoose Schema looks like this:
var StatusScheme = new Schema ({
StatusName: {type: String },
isClosed: {type:Boolean},
Questions:[QuestionSchema]
});
var QuestionSchema = new Schema ({
QuestionName: {type: String },
isRequired: {type:Boolean},
QuestionType:{type: String }
});
Now in my node.js app I want to iterate the schema to generate a tree of field names:
+StatusName
+isClosed
+Questions
+QuestionName
+isRequired
+QuestionType
I was exploring in the .modelSchemas[schema].tree object and I can get all of my field names the problem is I can't detect if the Questions array is really a different schema. Does anyone have any insight into the object that may tell me this? Once I know that a field is really a subdocument I can recursivly iterate the entire schema to build my tree.
I think I may have found the link. I can take a look modelSchemas object and dig into each path looking to see if the path has a caster object. If it does I can then fill it with the sub document data.
isClosed is not a subDocument and Questions is a subdocument. It looks like Mongoose then includes the constructor for in in the modelschema. Any thoughts on a better way to find the "tree" view or sub document relation within Mongoose.
المحلول
Details can be found @ https://groups.google.com/forum/#!topic/mongoose-orm/4sBbi388msI
A Child schema must be defined before it is embedded as a sub document.
To find the sub document schema traverse to "CaseSchema.paths["MYRecipients"].options.type[0]"
The tree property also contains the nested relationship between schemas.