在mongoose中更新数据时,我似乎无法传入选项,我只是得到一个错误,说Expected 0-2 arguments, but got 3.ts(2554)
我使用的代码和文档几乎一样,我使用的是mongoose的最新版本(8.0.3)。
const guild = await guildSchema.findOne({ id: interaction.guildId })
await guild?.updateOne(
{
id: interaction.user.id
},
{
$set: {
"some.value": true
}
},
{
arrayFilters: [
{
"i.recipe": "Fried rice",
"i.item": { $not: { $regex: "oil" } },
}
]
}
)
字符串
让我如此沮丧的是,根据文档(https://mongoosejs.com/docs/api/query.html#Query.prototype.updateOne()),这段代码很好,但它仍然抛出错误。
根据文档,这个方法甚至应该有4个参数:
Parameters:
[filter] «Object»
[update] «Object|Array» the update command
[options] «Object»
[options.multipleCastError] «Boolean» by default, mongoose only returns the first error that occurred in casting the query. Turn on this option to aggregate all the cast errors.
[options.strict] «Boolean|String» overwrites the schemas strict mode option
[options.upsert=false] «Boolean» if true, and no documents found, insert a new document
[options.writeConcern=null] «Object» sets the write concern for replica sets. Overrides the schema-level write concern
[options.timestamps=null] «Boolean» If set to false and schema-level timestamps are enabled, skip timestamps for this update. Note that this allows you to overwrite timestamps. Does nothing if schema-level timestamps are not set.
[options.translateAliases=null] «Boolean» If set to true, translates any schema-defined aliases in filter, projection, update, and distinct. Throws an error if there are any conflicts where both alias and raw property are defined on the same object.
[callback] «Function» params are (error, writeOpResult)
型
1条答案
按热度按时间ig9co6j11#
mongoose中的
Model
、Document
和Query
类之间存在差异。但是,Model
类是Document
类的子类。你认为你正在使用
Query.updateOne()
,它需要3个参数和一个回调。你实际上使用的是
Document.updateOne()
,它只接受2个参数和一个回调。举例说明:
字符串
我认为您应该只使用
Model.findOneAndUpdate()
,其中Guild
是使用const Guild = mongoose.model('Guild', guildSchema);
创建的Model
的示例型