toObject的Mongoose文档列出了功能,选项,并提供了功能的示例toObject.
Mongoose文档toJSON说明选项与toObject相同,但没有解释是什么toJSON. 在其他地方,文件说
toJSON与toObject选项完全相同,但仅在调用文档的toJSON方法时适用.
是toJSON别名toObject吗?如果没有,有什么区别?
Joh*_*yHK 18
看一下源代码就会发现两个方法都调用了一个内部$toObject方法,但是toJSON传递了第二个true参数:
Document.prototype.toObject = function (options) {
return this.$toObject(options);
};
...
Document.prototype.toJSON = function (options) {
return this.$toObject(options, true);
};
Run Code Online (Sandbox Code Playgroud)
第二个参数确定是$toObject使用默认值toJSON还是toObject模式选项.因此,除非这些模式选项的配置不同,否则这两种方法是相同的.
正如JohnnyHK的回答者所说,toJSON和之间没有区别toObject。我的猜测是,toJSON创建该JSON.stringify方法是为了支持该方法。
从MDN文档开始,如果对象具有toJSON作为函数的属性,JSON.stringify则将使用该toJSON函数序列化对象而不是对象本身。
对于我的用例,这就是我得到的:
.toJSON的优点是它被自动使用JSON.stringify。如果将架构选项设置toJSON为重塑选项,则当您使用该架构对文档进行字符串化时,将构建具有正确形状的对象
.toObject的优点是有时JSON.stringify会在断开文档和模式之间的链接后运行,因此不会发生重新整形。在这种情况下,您只需使用toObject这些选项调用文档方法即可获取具有正确形状的对象。
例子
const reshapingOptions = {
// include .id (it's a virtual)
virtuals: true,
// exclude .__v
versionKey: false,
// exclude ._id
transform: function (doc, ret) {
delete ret._id;
return ret;
},
};
const friendSchema = mongoose.Schema({
givenName: String,
familyName: String,
}, { toJSON: reshapingOptions });
const friendModel = mongoose.model('Friend', friendSchema);
const john = friendModel.findOne({ givenName: 'John' });
if (!john) {
res.status(404).json({ error: 'No John Found' });
}
Run Code Online (Sandbox Code Playgroud)
该声明
JSON.stringify(john);
Run Code Online (Sandbox Code Playgroud)
回到:
{
"id": "...",
"givenName": "John",
"familyName": "Doe"
}
Run Code Online (Sandbox Code Playgroud)
但这同样无辜的声明
JSON.stringify({
...john, // the spread breaks the link
role: 'dummy friend'
})
Run Code Online (Sandbox Code Playgroud)
突然回道:
{
"_id": "...",
"givenName": "John",
"familyName": "Doe",
"__v": 0,
"role": "dummy friend"
}
Run Code Online (Sandbox Code Playgroud)
所以我用了
res.json({
...john.toObject(reshapingOptions),
role: 'dummy friend'
})
Run Code Online (Sandbox Code Playgroud)
要得到:
{
"id": "...",
"givenName": "John",
"familyName": "Doe",
"role": "dummy friend"
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5458 次 |
| 最近记录: |