我有一个用例,我想用虚拟定义一个架构,然后在多个地方使用该架构。为此,我在猫鼬文档中找到了.add属性,如here所示。
这不是我期望的行为,默认情况下,虚拟机不会填充。我查看了生成的特定架构,发现使用“ .add”创建它后,它不会保留原始架构的所有字段。特别是在“虚拟”对象中,“路径”属性变为空对象而不是字符串,而在“树”对象中,虚拟完全消失了。
我仔细检查了实际的依赖关系,发现这是“ .add”内部工作方式的结果,即它调用了一个名为“ merge”的函数,该函数将许多属性复制到新架构上。值得注意的是,合并功能对架构上的“树”对象不执行任何操作,对于虚拟机,它会复制名称/选项/获取器/设置程序,但对“路径”不执行任何操作。我手动将缺少的部分添加到了合并功能中,它解决了我的问题,但是我想知道我是不是使用不正确,还是这是猫鼬库中的错误,因为直接更新依赖项不是对我来说有效的解决方案。
这是我实例化架构的一个例子
const testBase= data.db.Schema({
test: {type: String, required: true}
});
testBase.virtual('testVirtual')
.get(function () {
return "test";
});
testBase.set('toObject', {virtuals: true});
testBase.set('toJSON', {virtuals: true});
const testParent = data.db.Schema();
testParent.add(testBase);
testParent.set('toObject', {virtuals: true});
testParent.set('toJSON', {virtuals: true});
在此示例中,如果我在模型中使用“ testBase”作为架构,则它将正确使用虚拟,而如果使用“ testParent”,则不会。当我期望它们的行为相同时。
获取两个模式的JSON可以在testBase上看到
{
"$id": 24,
"_indexes": [],
"_userProvidedOptions": {
"toJSON": {
"virtuals": true
},
"toObject": {
"virtuals": true
}
},
"aliases": {},
"callQueue": [],
"childSchemas": [],
"inherits": {},
"methodOptions": {},
"methods": {},
"nested": {},
"obj": {
"test": {
"required": true
}
},
"options": {
"_id": true,
"autoIndex": null,
"bufferCommands": true,
"capped": false,
"discriminatorKey": "__t",
"id": true,
"minimize": true,
"noId": false,
"noVirtualId": false,
"read": null,
"shardKey": null,
"strict": true,
"toJSON": {
"virtuals": true
},
"toObject": {
"virtuals": true
},
"typeKey": "type",
"validateBeforeSave": true,
"versionKey": "__v"
},
"paths": {
"_id": {
"$immutable": null,
"_index": null,
"getters": [],
"instance": "ObjectID",
"options": {
"auto": true
},
"path": "_id",
"setters": [
null
],
"validators": []
},
"test": {
"$immutable": null,
"_index": null,
"enumValues": [],
"getters": [],
"instance": "String",
"isRequired": true,
"options": {
"required": true
},
"originalRequiredValue": true,
"path": "test",
"regExp": null,
"setters": [],
"validators": [
{
"message": "Path `{PATH}` is required.",
"type": "required"
}
]
}
},
"plugins": [],
"query": {},
"s": {
"hooks": {
"_posts": {},
"_pres": {}
}
},
"singleNestedPaths": {},
"statics": {},
"subpaths": {},
"tree": {
"_id": {
"auto": true
},
"test": {
"required": true
},
"testVirtual": {
"getters": [
null
],
"options": {},
"path": "testVirtual",
"setters": []
}
},
"virtuals": {
"testVirtual": {
"getters": [
null
],
"options": {},
"path": "testVirtual",
"setters": []
}
}
}
而这对于testParent
{
"$id": 25,
"_indexes": [],
"_userProvidedOptions": {
"toJSON": {
"virtuals": true
},
"toObject": {
"virtuals": true
}
},
"aliases": {},
"callQueue": [],
"childSchemas": [],
"inherits": {},
"methodOptions": {},
"methods": {},
"nested": {},
"options": {
"_id": true,
"autoIndex": null,
"bufferCommands": true,
"capped": false,
"discriminatorKey": "__t",
"id": true,
"minimize": true,
"noId": false,
"noVirtualId": false,
"read": null,
"shardKey": null,
"strict": true,
"toJSON": {
"virtuals": true
},
"toObject": {
"virtuals": true
},
"typeKey": "type",
"validateBeforeSave": true,
"versionKey": "__v"
},
"paths": {
"_id": {
"$immutable": null,
"_index": null,
"getters": [],
"instance": "ObjectID",
"options": {
"auto": true
},
"path": "_id",
"setters": [
null
],
"validators": []
},
"test": {
"$immutable": null,
"_index": null,
"enumValues": [],
"getters": [],
"instance": "String",
"isRequired": true,
"options": {
"required": true
},
"originalRequiredValue": true,
"path": "test",
"regExp": null,
"setters": [],
"validators": [
{
"message": "Path `{PATH}` is required.",
"type": "required"
}
]
}
},
"plugins": [],
"query": {},
"s": {
"hooks": {
"_posts": {},
"_pres": {}
}
},
"singleNestedPaths": {},
"statics": {},
"subpaths": {},
"tree": {
"_id": {
"auto": true
},
"test": {
"required": true
}
},
"virtuals": {
"testVirtual": {
"getters": [
null
],
"options": {},
"path": {},
"setters": []
}
}
}
如果您比较缺少的架构,则可以看到哪些字段丢失。如果有人知道按照这些方式使用此功能或示例的正确方法,我将不胜感激。 (我的测试是使用猫鼬5.7.8版完成的)