【发布时间】:2016-02-07 17:30:01
【问题描述】:
我正在从我的架构中构建一个字段名称及其类型的对象:
var ret = {};
ThisCollection.schema.eachPath(function(path) {
ret[path] = ThisCollection.schema.path(path).instance;
});
这很好,除非架构中有嵌套数组。我似乎无法弄清楚如何访问子文档及其字段的类型。我试过了:
ThisCollection.schema.path("dot.notation").instance
在递归函数中构建更深路径的点符号名称。这似乎不起作用。
示例架构:
var Person = new Schema({
person_code: String,
person_name: String,
location_details:[{
location_name: String,
location_code: String
}]
});
明确地说,我正在寻找我的返回对象以在结构上与我的架构相匹配,以便嵌套架构是我的返回对象中的嵌套对象,例如:
{
person_code: String,
person_name: String,
location_details:{
location_name: String,
location_code: String
}
}
【问题讨论】:
-
@JohnnyHK 为清楚起见更新了问题
-
您已经忘记了
location_details是一个数组这一事实,这是您想要的吗?你最终想用这个做什么?这对我来说没有多大意义。如果您需要,为什么不直接保存原始架构定义对象? -
@JohnnyHK 是的,我故意丢失了数组。稍后我需要另一个功能才能“读取”我声明的架构(字段名称及其声明的类型),并且我的架构有时是用户定义的。我对 Mongo/Mongoose/Node 还很陌生,所以我可能遗漏了很多小东西。您可能会使用原始模式定义对象进行某些操作,这就是我尝试使用
schema.paths访问的内容 -
@JohnnyHK 你是在正确的道路上,我正在寻找的是
ThisCollection.schema.tree,你想发布一个答案然后为赏金?