问题描述
toObject的Mongoose文档列出了功能,选项并提供了示例toObject
的功能.
The Mongoose documentation for toObject lists features, options, and gives examples of functionality of toObject
.
toJSON
的Mongoose文档说这些选项与toObject相同,但是没有解释toJSON
的作用. 文档说的其他地方
The Mongoose documentation for toJSON
says the options are the same as toObject, but doesn't explain what toJSON
does. Elsewhere the documentation says
toJSON
是toObject
的别名吗?如果没有,有什么区别?
Is toJSON
an alias of toObject
? If not, what are the differences?
推荐答案
看看源代码揭示了这两种方法都调用内部的$toObject
方法,但是toJSON
传递了第二个true
参数:
A look at the source code reveals that both methods call an internal $toObject
method, but with toJSON
passing a second, true
parameter:
Document.prototype.toObject = function (options) {
return this.$toObject(options);
};
...
Document.prototype.toJSON = function (options) {
return this.$toObject(options, true);
};
第二个参数确定$toObject
是使用toJSON
还是toObject
架构选项作为默认值.因此,除非这些架构选项的配置不同,否则这两种方法是相同的.
That second parameter determines whether $toObject
uses the toJSON
or toObject
schema options for its defaults. So unless those schema options are configured differently, the two methods are identical.
这篇关于Mongoose toObject和toJSON有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!