Mongoose如何使用.add使用虚拟机重用架构

Mongoose如何使用.add使用虚拟机重用架构,mongoose,mongoose-schema,mongoose-populate,Mongoose,Mongoose Schema,Mongoose Populate,我有一个用例,我想定义一个虚拟的模式,然后在多个地方使用该模式。为此,我在mongoose文档中找到了.add属性,如图所示 不过,这种行为并不是我所期望的,默认情况下,虚拟机不会填充。我查看了生成的特定模式,发现在使用“.add”创建它之后,它并没有保留原始模式的所有字段。特别是在“virtuals”对象中,“path”属性变为空对象而不是字符串,而在“tree”对象中,虚拟对象完全丢失 我查看了实际的依赖关系,发现这是“.add”如何在内部工作的结果,即它调用了一个名为“merge”的函数,

我有一个用例,我想定义一个虚拟的模式,然后在多个地方使用该模式。为此,我在mongoose文档中找到了.add属性,如图所示

不过,这种行为并不是我所期望的,默认情况下,虚拟机不会填充。我查看了生成的特定模式,发现在使用“.add”创建它之后,它并没有保留原始模式的所有字段。特别是在“virtuals”对象中,“path”属性变为空对象而不是字符串,而在“tree”对象中,虚拟对象完全丢失

我查看了实际的依赖关系,发现这是“.add”如何在内部工作的结果,即它调用了一个名为“merge”的函数,该函数将许多属性复制到新模式中。值得注意的是,merge函数对模式上的“tree”对象不做任何处理,对于虚拟对象,它复制名称/options/getter/setter,但对“path”不做任何处理。我手动将缺少的部分添加到合并函数中,它修复了我的问题,但我想知道我是否只是错误地使用了它,或者这是mongoose库中的一个错误,因为直接更新依赖项对我来说不是一个有效的解决方案

下面是我如何实例化模式的示例

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": []
    }
}
}

如果比较缺少的模式,可以看到缺少哪些字段。如果有人知道使用此功能的正确方法或这些方面的示例,我将不胜感激。(我的测试是用mongoose 5.7.8版完成的)