弃用警告
¥Deprecation Warnings
Mongoose 用户应该注意 MongoDB Node.js 驱动程序 中的一些弃用内容。Mongoose 提供了解决这些弃用警告的选项,但你需要测试这些选项是否会给你的应用带来任何问题。请 在 GitHub 上报告任何问题。
¥There are several deprecations in the MongoDB Node.js driver that Mongoose users should be aware of. Mongoose provides options to work around these deprecation warnings, but you need to test whether these options cause any problems for your application. Please report any issues on GitHub.
概括
¥Summary
要修复所有弃用警告,请按照以下步骤操作:
¥To fix all deprecation warnings, follow the below steps:
在
findOneAndUpdate()
、findOneAndReplace()
、findOneAndDelete()
调用中将rawResult: true
替换为includeResultMetadata: true
。¥Replace
rawResult: true
withincludeResultMetadata: true
infindOneAndUpdate()
,findOneAndReplace()
,findOneAndDelete()
calls.
请阅读下文,了解每个弃用警告的更详细说明。
¥Read below for more a more detailed description of each deprecation warning.
rawResult
从 Mongoose 7.4.0 开始,rawResult
到 findOneAndUpdate()
选项已被弃用。你应该使用 includeResultMetadata
选项,这是 MongoDB Node.js 驱动程序的新选项,用于替换 rawResult
。
¥As of Mongoose 7.4.0, the rawResult
option to findOneAndUpdate()
is deprecated.
You should instead use the includeResultMetadata
option, which the MongoDB Node.js driver's new option that replaces rawResult
.
// Replace this:
const doc = await Test.findOneAndUpdate(
{ name: 'Test' },
{ name: 'Test Testerson' },
{ rawResult: true }
);
// With this:
const doc = await Test.findOneAndUpdate(
{ name: 'Test' },
{ name: 'Test Testerson' },
{ includeResultMetadata: true }
);
rawResult
选项仅影响 Mongoose;MongoDB Node.js 驱动程序仍然返回完整的结果元数据,Mongoose 只是解析出原始文档。includeResultMetadata
选项还告诉 MongoDB Node.js 驱动程序仅返回文档,而不是完整的 ModifyResult
对象。
¥The rawResult
option only affects Mongoose; the MongoDB Node.js driver still returns the full result metadata, Mongoose just parses out the raw document.
The includeResultMetadata
option also tells the MongoDB Node.js driver to only return the document, not the full ModifyResult
object.